我在WordPress的编辑帖子部分创建了一个自定义字段,并且能够通过以下帖子保存值:Wordpress - 在帖子屏幕中添加自定义字段.
我能够检索特定类别的帖子,但我无法检索自定义字段的值.
如上图所示,左上角突出显示了一个自定义帖子字段.另一个突出显示的字段是显示该帖子属于"投资组合"类别.
这是我用来检索"投资组合"类别帖子的代码
<?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();
?>
Run Code Online (Sandbox Code Playgroud)
我能够获得帖子标题的值和帖子的内容,但不能获得自定义字段值.我的代码有什么问题?
你可以使用get_post_custom($ post_id)
在你的情况下
while ( $the_query->have_posts() ) :
$the_query->the_post();
$custom = get_post_custom( get_the_ID() ); ?>
Run Code Online (Sandbox Code Playgroud)
然后$custom
是自定义字段的数组
归档时间: |
|
查看次数: |
25138 次 |
最近记录: |