get_the_id与post-> ID与the_id/get_post_meta [英] get_the_id vs. post->ID vs. the_id / get_post_meta
问题描述
我认为这肯定是一个非常基本的问题,但我只是在开始.有人可以看一下下面相同代码(?)的3个版本,并说出区别吗?他们似乎都在我正在工作的循环中正常工作.
I think it must be pretty basic question but I am only starting. Can someone have a look at the 3 versions of the same (?) code below and say what the difference is? All of them seem to work fine in the loop I am working on.
应使用以下哪个:$post->ID
,$the_ID
或get_the_id()
?
global $post;
是否有必要?
Which should be used: $post->ID
, $the_ID
or get_the_id()
?
Is it necessary to have 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()
If you're inside a WordPress loop, then $post->ID
it's the same as using get_the_ID()
您不必全球化$post
,因为它已经在WordPress循环范围之内.
You shouldn't need to globalize $post
since it's already in the scope of a WordPress loop.
我从未见过使用$the_ID
编写的代码,因此我会避免使用它.
I've never seen code using $the_ID
, so I would avoid using that.
最安全的选择是使用get_the_ID()
这篇关于get_the_id与post-> ID与the_id/get_post_meta的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!