• Resolved laptopsticker

    (@laptopsticker)


    I’m trying to display some tabbed content where each tab is a taxonomy term. And under each tab there would be a loop displaying posts from that taxonomy term.

    Right now I can only display posts from a specific taxonomy term. In this case “Hotels”.

    How do I modify my code so that all terms would be displayed with their posts underneath?

    Hotels
    – First hotel post
    – Second hotel post
    Cafes
    – First cafe post
    – Second cafe post

    <?php
    $args = array(
      'post_type'   => 'testimonials',
      'post_status' => 'publish',
      'tax_query'   => array(
      	array(
      		'taxonomy' => 'testimonial_taxonomies',
      		'field'    => 'slug',
      		'terms'    => 'hotels'
      	)
      )
     );
     
    $testimonials = new WP_Query( $args );
    if( $testimonials->have_posts() ) :
    ?>
    
        <?php
          while( $testimonials->have_posts() ) :
            $testimonials->the_post();
            ?>
    			<?php the_title( '<h3>', '</h3>' ); ?>
    			<?php the_excerpt(); ?>
            <?php
          endwhile;
          wp_reset_postdata();
        ?>
    
    <?php
    else : ?> 
    <? endif;?>
Viewing 1 replies (of 1 total)
  • Thread Starter laptopsticker

    (@laptopsticker)

    EDIT: Never mind the below text… the below code works if you just substitute “category” for your custom taxonomy and “post” for your custom post type.

    I’m leaving this in case someone else needs this solution.

    I also found this code, but it only displays the taxonomy terms, not the posts.

    If the two codes could somehow be combined… then I’d have what I need.

    <?php
    $cat_terms = get_terms(
                    array('category'),
                    array(
                            'hide_empty'    => false,
                            'orderby'       => 'name',
                            'order'         => 'ASC',
                            'number'        => 6 //specify yours
                        )
                );
    
    if( $cat_terms ) :
    
        foreach( $cat_terms as $term ) :
    
            //var_dump( $term );
            echo '<h3>'. $term->name .'</h3>';
    
            $args = array(
                    'post_type'             => 'post',
                    'posts_per_page'        => 10, //specify yours
                    'post_status'           => 'publish',
                    'tax_query'             => array(
                                                array(
                                                    'taxonomy' => 'category',
                                                    'field'    => 'slug',
                                                    'terms'    => $term->slug,
                                                ),
                                            ),
                    'ignore_sticky_posts'   => true //caller_get_posts is deprecated since 3.1
                );
            $_posts = new WP_Query( $args );
    
            if( $_posts->have_posts() ) :
                while( $_posts->have_posts() ) : $_posts->the_post();
    
                    echo '<h5>'. get_the_title() .'</h5>';
    
                endwhile;
            endif;
            wp_reset_postdata(); //important
    
        endforeach;
    
    endif;
Viewing 1 replies (of 1 total)
  • The topic ‘Display a list of custom taxonomy terms and their posts’ is closed to new replies.