get_the_id vs. post-> ID vs. the_id/get_post_meta

The*_*ear 9 php variables wordpress global

我认为这一定是非常基本的问题,但我只是开始.有人可以查看下面相同(?)代码的3个版本,并说明有什么区别?所有这些似乎都在我正在研究的循环中正常工作.

哪个应该使用:$post->ID,$the_IDget_the_id()?有必要global $post;吗?

global $post;
$content = get_post_meta( $post->ID, ‘my_custom_field', true );
echo  $content;
Run Code Online (Sandbox Code Playgroud)

要么

$content = get_post_meta( $the_ID, ‘my_custom_field', true );
echo  $content;
Run Code Online (Sandbox Code Playgroud)

要么

$content = get_post_meta( get_the_id(), ‘my_custom_field’, true );
echo  $content;
Run Code Online (Sandbox Code Playgroud)

非常感谢您的帮助

小智 8

如果你在WordPress循环中,那么$post->ID它与使用相同get_the_ID()

您不应该全球化,$post因为它已经在WordPress循环的范围内.

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

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