最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

custom post types - Looping Through Categories of a CPT

programmeradmin2浏览0评论

I am trying to create tabs using the Foundations framework, outputting posts from a custom post type called testimonial and filtering them through their assigned custom category called filed-under. What am I doing wrong in my loop?

What I Have Working

  • Category tabs appear when new categories are created

What is Not Working

  • The posts aren't appearing in the tabbed content area
  • How do you get the loop to filter the 'all' category?

Link to Demo /testimonials/

Code

        <?php
        // TABBED HEADERS
        echo '<ul class="tabs" data-tabs id="example-tabs">';
        echo '<li class="tabs-title is-active link-no-style">';
        echo '<a href="#all" aria-selected="true">All</a>';
        echo '</li>';
        $args = array(
          'hide_empty' => 1,
          'orderby'   => 'name',
          'order'     => 'ASC',
          'post_type' => 'testimonial',
          'taxonomy'  => 'filed-under',
        );
        $categories = get_categories($args);
        foreach ($categories as $category) {
          echo
            '<li class="tabs-title link-no-style">
                <a href="#' . $category->slug . '" aria-selected="true" role="tab" data-toggle="tab">    
                  ' . $category->name . '
                </a>
              </li>';
        }
        echo '</ul>';

        // TABBED CONTENT
        echo '<div class="tabs-content" data-tabs-content="example-tabs">';
        echo '<div class="tabs-panel is-active" id="' . $category->slug . '">';
        echo '<ul class="accordion" data-accordion data-allow-all-closed="true">';
        foreach ($categories as $category) {
          $the_query = new WP_Query(array(
            'post_type'     => 'testimonial',
            'post_status'   => 'publish',
            'category_name' => $category->slug,
          ));
          while ($the_query->have_posts()) : $the_query->the_post();
            echo '<li class="accordion-item" data-accordion-item>';
            echo '<a href="#" class="accordion-title"><p>';
            the_title();
            echo '</p></a>';
            echo '<div class="accordion-content" data-tab-content>';
            echo the_field('testimonial');
            echo '</div>';
            echo '</li>';
          endwhile;
          wp_reset_postdata();
        }
        echo '</ul>';
        echo '</div>';
        echo '</div>';
        ?>

CODE V2

<?php 
        echo '<ul class="tabs" data-tabs id="example-tabs">';
        echo '<li class="tabs-title is-active link-no-style">';
        echo '<a href="#" aria-selected="true">All</a>';
        echo '</li>';  

        $_terms = get_terms(array('filed-under'));
        foreach ($_terms as $term) {

        // TABBED HEADERS 
        echo '<li class="tabs-title link-no-style">';
        echo '<a href="#' . $term->slug . '"aria-selected="true" role="tab" data-toggle="tab">';
        echo $term->name;
        echo '</a>';
        echo '</li>';
        }  // CLOSE OFF FIRST LOOP 
        echo '</ul>';

        foreach ($_terms as $term) :
        $term_slug = $term->slug;
        
        // QUERY
        $_posts = new WP_Query( array(
          'post_type'         => 'testimonial',
          'posts_per_page'    => -1, 
          'tax_query' => array(
            array(
              'taxonomy' => 'filed-under',
              'field'    => 'slug',
              'terms'    => $term_slug,
            ),
          ),
        )); 
        
        // TABBED CONTENT
        echo '<div class="tabs-content" data-tabs-content="example-tabs">';
        echo '<div class="tabs-panel" id="' . $term_slug . '">';
        echo '<ul class="accordion" data-accordion data-allow-all-closed="true">';
        if( $_posts->have_posts() ) :  while ( $_posts->have_posts() ) : $_posts->the_post(); 
        echo '<li class="accordion-item" data-accordion-item>';
        echo '<a href="#" class="accordion-title">';
        echo '<p>';
        the_title(); 
        echo '</p>';
        echo '</a>';
        echo '<div class="accordion-content" data-tab-content>';
        echo the_field('testimonial');
        echo '</div>';
        echo '</li>'; 
        endwhile; endif; wp_reset_postdata();
        echo '</ul>';
        echo '</div>';
        echo '</div>';
        endforeach; 
        ?>

I am trying to create tabs using the Foundations framework, outputting posts from a custom post type called testimonial and filtering them through their assigned custom category called filed-under. What am I doing wrong in my loop?

What I Have Working

  • Category tabs appear when new categories are created

What is Not Working

  • The posts aren't appearing in the tabbed content area
  • How do you get the loop to filter the 'all' category?

Link to Demo http://staging-newmummycompanyca.temp312.kinsta.cloud/testimonials/

Code

        <?php
        // TABBED HEADERS
        echo '<ul class="tabs" data-tabs id="example-tabs">';
        echo '<li class="tabs-title is-active link-no-style">';
        echo '<a href="#all" aria-selected="true">All</a>';
        echo '</li>';
        $args = array(
          'hide_empty' => 1,
          'orderby'   => 'name',
          'order'     => 'ASC',
          'post_type' => 'testimonial',
          'taxonomy'  => 'filed-under',
        );
        $categories = get_categories($args);
        foreach ($categories as $category) {
          echo
            '<li class="tabs-title link-no-style">
                <a href="#' . $category->slug . '" aria-selected="true" role="tab" data-toggle="tab">    
                  ' . $category->name . '
                </a>
              </li>';
        }
        echo '</ul>';

        // TABBED CONTENT
        echo '<div class="tabs-content" data-tabs-content="example-tabs">';
        echo '<div class="tabs-panel is-active" id="' . $category->slug . '">';
        echo '<ul class="accordion" data-accordion data-allow-all-closed="true">';
        foreach ($categories as $category) {
          $the_query = new WP_Query(array(
            'post_type'     => 'testimonial',
            'post_status'   => 'publish',
            'category_name' => $category->slug,
          ));
          while ($the_query->have_posts()) : $the_query->the_post();
            echo '<li class="accordion-item" data-accordion-item>';
            echo '<a href="#" class="accordion-title"><p>';
            the_title();
            echo '</p></a>';
            echo '<div class="accordion-content" data-tab-content>';
            echo the_field('testimonial');
            echo '</div>';
            echo '</li>';
          endwhile;
          wp_reset_postdata();
        }
        echo '</ul>';
        echo '</div>';
        echo '</div>';
        ?>

CODE V2

<?php 
        echo '<ul class="tabs" data-tabs id="example-tabs">';
        echo '<li class="tabs-title is-active link-no-style">';
        echo '<a href="#" aria-selected="true">All</a>';
        echo '</li>';  

        $_terms = get_terms(array('filed-under'));
        foreach ($_terms as $term) {

        // TABBED HEADERS 
        echo '<li class="tabs-title link-no-style">';
        echo '<a href="#' . $term->slug . '"aria-selected="true" role="tab" data-toggle="tab">';
        echo $term->name;
        echo '</a>';
        echo '</li>';
        }  // CLOSE OFF FIRST LOOP 
        echo '</ul>';

        foreach ($_terms as $term) :
        $term_slug = $term->slug;
        
        // QUERY
        $_posts = new WP_Query( array(
          'post_type'         => 'testimonial',
          'posts_per_page'    => -1, 
          'tax_query' => array(
            array(
              'taxonomy' => 'filed-under',
              'field'    => 'slug',
              'terms'    => $term_slug,
            ),
          ),
        )); 
        
        // TABBED CONTENT
        echo '<div class="tabs-content" data-tabs-content="example-tabs">';
        echo '<div class="tabs-panel" id="' . $term_slug . '">';
        echo '<ul class="accordion" data-accordion data-allow-all-closed="true">';
        if( $_posts->have_posts() ) :  while ( $_posts->have_posts() ) : $_posts->the_post(); 
        echo '<li class="accordion-item" data-accordion-item>';
        echo '<a href="#" class="accordion-title">';
        echo '<p>';
        the_title(); 
        echo '</p>';
        echo '</a>';
        echo '<div class="accordion-content" data-tab-content>';
        echo the_field('testimonial');
        echo '</div>';
        echo '</li>'; 
        endwhile; endif; wp_reset_postdata();
        echo '</ul>';
        echo '</div>';
        echo '</div>';
        endforeach; 
        ?>
Share Improve this question edited Nov 14, 2020 at 1:15 Natasha Mcd asked Nov 11, 2020 at 14:35 Natasha McdNatasha Mcd 1257 bronze badges 2
  • Can you fix the indenting on your code? It's difficult to read, there are free code editors programs that will do indenting automatically, such as Sublime Text, or Visual Studio Code – Tom J Nowell Commented Nov 11, 2020 at 16:34
  • 1 Thanks for the tip! I downloaded a plugin for Visual Code to help me in the future and have updated above. – Natasha Mcd Commented Nov 11, 2020 at 17:42
Add a comment  | 

1 Answer 1

Reset to default 1
' publish',

This is your problem, an extra space character.

Note that in future you can catch this by using Query Monitor and by checking if posts were found. Because the post loop is missing an if ( $query->have_posts() ) { ... } else { echo "none found"; } type check, you had no way of knowing where to look for the problem.

Also, you're listing terms in the filed-under taxonomy:

'taxonomy'  => 'filed-under',

But then you're passing those terms to the category_name parameter, which is unrelated. So now the terms are being used as if they are terms in the category taxonomy, but they are not.

发布评论

评论列表(0)

  1. 暂无评论