【问题标题】:Get the value of a custom field of a WordPress post获取 WordPress 帖子的自定义字段的值
【发布时间】:2013-03-09 13:33:32
【问题描述】:

我在 WordPress 的编辑帖子部分创建了一个自定义字段,并且可以通过以下帖子保存该值:Wordpress - Adding custom field to the post screen

我可以检索特定类别的帖子,但我无法检索自定义字段的值。

如上图所示,左上角突出显示了一个自定义帖子字段。另一个突出显示的字段是表明该帖子属于“投资组合”类别。

这是我用来检索“投资组合”类别帖子的代码

<?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();
?>

我能够获取帖子标题和帖子内容的值,但不能获取自定义字段值。我的代码有什么问题?

【问题讨论】:

标签: php wordpress


【解决方案1】:

您可以使用get_post_custom( $post_id )

你的情况

while ( $the_query->have_posts() ) :
    $the_query->the_post();
    $custom = get_post_custom( get_the_ID() ); ?>

那么$custom 是您的自定义字段的数组

【讨论】:

  • 有没有一种简单的方法可以在与帖子查询相同的 SQL 调用中获取帖子的自定义字段? (优化)
  • 我也想知道,是否可以通过帖子的初始查询来完成?
猜你喜欢
  • 1970-01-01
  • 2012-07-22
  • 1970-01-01
  • 2018-12-24
  • 1970-01-01
  • 2019-05-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多