我认为这一定是非常基本的问题,但我才刚刚开始。有人可以看看下面相同(?)代码的3个版本并说出区别是什么吗?所有这些似乎都在我正在处理的循环中工作正常。
应该使用哪个:$post->ID
、$the_ID
或get_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()