【问题标题】:How to Get The Taxonomy Term in Custom Post Type Loop Inside a Wp Query如何在 Wp 查询内的自定义帖子类型循环中获取分类术语
【发布时间】:2022-01-06 19:26:32
【问题描述】:

我需要在运行 WP Query 时获取每个帖子的 term。我在循环内试过这个

$term = $loop->get_queried_object();
echo  $term->name; 

但我仍然获得自定义帖子类型注册名称而不是获得期限!

$args = array(  
        'post_type' => 'services',
        'post_status' => 'publish',
        'posts_per_page' => 8, 
        'orderby' => 'title', 
        'order' => 'ASC', 
    );

    $loop = new WP_Query( $args ); 
        
    while ( $loop->have_posts() ) : $loop->the_post(); 

     $term = $loop->get_queried_object();
     echo  $term->name; 

     echo  get_the_title(); 

endwhile;
wp_reset_postdata(); 

【问题讨论】:

  • 嗨,这是自定义分类,但我需要的是 Term 而不是税

标签: wordpress custom-post-type


【解决方案1】:

您可以使用get_the_terms。试试下面的代码。

$args = array(  
    'post_type' => 'services',
    'post_status' => 'publish',
    'posts_per_page' => 8, 
    'orderby' => 'title', 
    'order' => 'ASC', 
);

$loop = new WP_Query( $args ); 
    
while ( $loop->have_posts() ) : $loop->the_post(); 

    $terms = get_the_terms( get_the_ID(), 'your-custom-taxonomy-name' );
    $terms = join(', ', wp_list_pluck( $terms , 'name') );

    echo $terms;

    echo  get_the_title(); 

endwhile;
wp_reset_postdata();

【讨论】:

  • 一如既往,很好的答案!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-07-01
  • 1970-01-01
  • 2014-09-04
  • 1970-01-01
  • 1970-01-01
  • 2014-07-12
  • 1970-01-01
相关资源
最近更新 更多