作者热门文章
- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我认为这一定是一个非常基本的问题,但我才刚刚开始。有人可以看看下面相同(?)代码的 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()
关于php - get_the_id 与 post->ID 与 the_id/get_post_meta,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26978722/
我认为这一定是一个非常基本的问题,但我才刚刚开始。有人可以看看下面相同(?)代码的 3 个版本并说出有什么区别吗?在我正在处理的循环中,它们似乎都运行良好。 应该使用哪个:$post->ID、$the
我是一名优秀的程序员,十分优秀!