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

标签 php wordpress variables global

我认为这一定是一个非常基本的问题,但我才刚刚开始。有人可以看看下面相同(?)代码的 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/

相关文章:

php - 错误 #1064 mysql 更新 wordpress wp_posts - post_content phpmyadmin

java - 变量似乎未初始化

JavaScript 网址变量

php - 多个 INSERT 的输出消息 - mysql

php - 使用 codeigniter 同时插入和更新

Wordpress 上的 css 列表定义不同,然后我希望它用于一个部分,我该如何修复它?

php - 使用PHP的“Notice: Undefined variable”,“Notice: Undefined index”和“Notice: Undefined offset”

php - 实现多个客户 list

php - 对于 PHP/MySQL 中的每 3 行

php - “WC_Abstract_Legacy_Order->__get, wc_doing_it_wrong” WooCommerce错误