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 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; 
        ?>
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. Commented Nov 11, 2020 at 17:42

1 Answer 1

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.

5
  • Removing the space didn't make a difference. I also added, if ( $the_query->have_posts()) : while ( $the_query->have_posts() ) : $the_query->the_post(); and that did not work, it broke the page from rendering at all. Would you mind providing me a little more guidance? Thank you! Commented Nov 11, 2020 at 17:18
  • You need an else case and at least an endif; if you're going to do that. I think you should stop trying to get the tabs working and instead focusing on making sure your queries work. Doing it all at once is too much. If you can get it working as headings + bullet lists, then you can rearrange the HTML to produce the tab HTML you desire.
    – Tom J Nowell
    Commented Nov 11, 2020 at 23:10
  • Also see the update I made to my answer, it looks like by using get_categories() and naming your variables $categories and $category, rather than using get_terms, $terms and $term, you've confused yourself into requesting categories when you're actually dealing with terms in the filed-under taxonomy, not terms in the category taxonomy. This is another reason your post queries aren't finding posts
    – Tom J Nowell
    Commented Nov 11, 2020 at 23:15
  • Thank you. I have made some progress, but now the only thing I have left, is funneling ALL of the posts into a tab called ALL. I don't even know where to begin with doing that. Updated code is shown above. Commented Nov 14, 2020 at 1:09
  • @NatashaMcd my answer tried to solve your original question. Remember this is a Q&A site, not a discussion thread, if my answer answers your question you should mark it as accepted. If you have a new question to ask, then you should create a new question, don't move the goal post by editinng your existing question to add new requirements
    – Tom J Nowell
    Commented Nov 14, 2020 at 1:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.