gpt4 book ai didi

php - get_the_id 与 post->ID 与 the_id/get_post_meta

转载 作者:可可西里 更新时间:2023-11-01 12:17:18 25 4
gpt4 key购买 nike

我认为这一定是一个非常基本的问题,但我才刚刚开始。有人可以看看下面相同(?)代码的 3 个版本并说出有什么区别吗?在我正在处理的循环中,它们似乎都运行良好。

应该使用哪个:$post->ID$the_IDget_the_id()?是否需要global $post;

global $post;
$content = get_post_meta( $post->ID, ‘my_custom_field', true );
echo $content;

$content = get_post_meta( $the_ID, ‘my_custom_field', true );
echo $content;

$content = get_post_meta( get_the_id(), ‘my_custom_field’, true );
echo $content;

非常感谢您的帮助

最佳答案

如果您在 WordPress 循环内,则 $post->ID 与使用 get_the_ID()

相同

您不需要全局化 $post,因为它已经在 WordPress 循环的范围内。

我从未见过使用 $the_ID 的代码,所以我会避免使用它。

最安全的选择是使用 get_the_ID()

关于php - get_the_id 与 post->ID 与 the_id/get_post_meta,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26978722/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com