php 获取 WordPress 帖子的自定义字段的值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15523844/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get the value of a custom field of a WordPress post
提问by Arish
I have created one custom field in WordPress' edit post section, and am able to save the value by following this posts: Wordpress - Adding custom field to the post screen.
我在 WordPress 的编辑帖子部分创建了一个自定义字段,并且能够通过关注以下帖子来保存该值:Wordpress - 将自定义字段添加到帖子屏幕。
I'm able to retrieve the posts of the specific category, but I'm unable to retrieve the value of custom field.
我能够检索特定类别的帖子,但无法检索自定义字段的值。


As seen in image above, there is a custom post field at highlighted at the top left. The other highlighted field is to show that the post belongs to the "Portfolio" category.
如上图所示,左上角突出显示了一个自定义帖子字段。另一个突出显示的字段是显示帖子属于“作品集”类别。
Here is the code I used to retrieve the posts of the category "Portfolio"
这是我用来检索类别“作品集”帖子的代码
<?php
$the_query = new WP_Query(array(
'category_name' => 'Portfolio',
'posts_per_page' => 9,
'order' => 'DESC'
));
while ( $the_query->have_posts() ) :
$the_query->the_post();
?>
<p>The title: <?php the_title(); ?></p>
<p> custome value: <?php get_post_meta( $post_ID, '_ssb_portfolio_url', true); ?> </p>
<p>The Content: <?php the_content(); ?></p>
<?php
endwhile;
wp_reset_postdata();
?>
I'm able to get the value of the title of the post and the content of the post, but not custom field value. What wrong in my code?
我能够获取帖子标题和帖子内容的值,但不能获取自定义字段值。我的代码有什么问题?
回答by Galen
You can use get_post_custom( $post_id )
您可以使用get_post_custom( $post_id )
In your case
在你的情况下
while ( $the_query->have_posts() ) :
$the_query->the_post();
$custom = get_post_custom( get_the_ID() ); ?>
Then $customis an array of your custom fields
然后$custom是您的自定义字段数组

