【问题标题】:How do I get the term name of current posts taxonomy?如何获取当前帖子分类的术语名称?
【发布时间】:2021-09-07 12:27:15
【问题描述】:

我正在尝试从分类平面区域中获取当前帖子的术语名称。 我想显示术语名称,然后输出该术语下的所有帖子 谁能帮帮我?

<?php 
$floorplan_area = get_sub_field('floorplan_area');

if ($floorplan_area) {
    if (!is_array($floorplan_area)) {
        $floorplan_area = array($floorplan_area);
    }
    $args = array(
        'post_type' => 'floorplan',
        'posts_per_page' => -1,
        'orderby' => 'post_title',
        'order' => 'ASC',
        'tax_query' => array(
            array(
                'taxonomy' => 'floorplanarea',
                'terms' => $floorplan_area,
            ),
        ),
    );
    $area_query = new WP_Query($args);
    if ($area_query->have_posts()) {?>

    /*Title of term name to go here*/

    <div>
    <?php 
        while($area_query->have_posts()) {
            $area_query->the_post(); ?>

            <h1><?php echo the_title(); ?></h1>

        <?php } ?>
    </div>
        <?php 
    }
    wp_reset_postdata();
}
?>

【问题讨论】:

    标签: php wordpress advanced-custom-fields


    【解决方案1】:

    找到解决办法

    $term = get_term( $floorplan_area, 'floorplanarea' );
    $name = $term->name;
    echo $name;
    

    【讨论】:

      【解决方案2】:

      一个 WP_Term 对象

      object(WP_Term) (11) {
          ["term_id"]=>  //int
          ["name"]=>   //string 
          ["slug"]=>  //string 
          ["term_group"]=>  //int
          ["term_taxonomy_id"]=> //int
          ["taxonomy"]=>   //string
          ["description"]=>    //string
          ["parent"]=> //int
          ["count"]=>  // int
          ["filter"]= //string
          ["meta"]= array(0) {} //an array of meta fields.
      }
      

      因为它是一个对象,你需要使用 foreach 循环

      $terms = get_terms($taxonomy);
      
      foreach ($terms as $term) {
      
          echo $term->name;
          echo $term->slug;
          
          $name = $term->name;
          echo 'My name is:' . $name . '!';
          
      }
      

      【讨论】:

        猜你喜欢
        • 2012-11-25
        • 1970-01-01
        • 1970-01-01
        • 2012-08-30
        • 1970-01-01
        • 1970-01-01
        • 2016-07-01
        • 2019-02-17
        • 2019-06-23
        相关资源
        最近更新 更多