skip to Main Content

I’m trying to get the term name of the current post from the taxonomy floorplanarea.
I want to display the term name and then output all posts under the term
Could anyone help me?

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

2

Answers


  1. Chosen as BEST ANSWER

    Found the solution

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

  2. A WP_Term object

    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.
    }
    

    As it’s an object you need to use a foreach loop

    $terms = get_terms($taxonomy);
    
    foreach ($terms as $term) {
    
        echo $term->name;
        echo $term->slug;
        
        $name = $term->name;
        echo 'My name is:' . $name . '!';
        
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search