Loop through categories and create tab for each WordPress and Bootstrap 3

I’m trying to create a page that holds all categories of my custom post type as tabs, with a tab content.

I am able to display all the category names as tabs, but i need to run a query in each tab content area to the corresponding category.

Read More

So when I click on tab named “1” the tab content area should only show posts from the category belonging to the tab named “1”.

My code so far:

 <?php
   echo '<ul class="nav nav-tabs" role="tablist">';

$args = array(
'hide_empty'=> 1,
'orderby' => 'name',
'order' => 'ASC'
 );

$categories = get_categories($args);

 foreach($categories as $category) { 

echo '<li><a href="#' . $category->name.'" role="tab" data-toggle="tab">' .      
$category->name.'</a></li>';
$cat_name = $category->name;

} 
echo '</ul>';
echo '<div class="tab-content">';
  foreach($categories as $category) { 
    echo '<div class="tab-pane" id="' . $category->name.'">';


    ?>

<?php 

 $the_query = new WP_Query(array(
  'post_type' => 'acme_product',
  'posts_per_page' => 100,
  'category_name' => $cat_name
    )); 
 while ( $the_query->have_posts() ) : 
 $the_query->the_post();
 ?>

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

               <?php 
endwhile; 

 ?>   


  <?php } 
echo '</div>';
 ?>

The problems is that each content area displays all post of every category.

What i want to achieve: Link

Any suggestions?

Related posts

Leave a Reply

2 comments

  1. ‘category_name’ parameter takes value as category slug. So you should use category slug in place of category name in the query.

    $cat_slug = $category->slug;
    
    $the_query = new WP_Query(array(
                 'post_type' => 'acme_product',
                 'posts_per_page' => 100,
                 'category_name' => $cat_slug
    )); 
    
  2. i asked for this problem in previous time but after some attempt for solving my problem i solved it by this perfect cod that work 100%100 with me

    <?php
        echo '<ul class="nav nav-tabs" role="tablist">';
        $args = array(
            'hide_empty'=> 1,
            'orderby' => 'name',
            'order' => 'ASC'
        );
        $categories = get_categories($args);
        foreach($categories as $category) { 
            echo 
                '<li>
                    <a href="#'.$category->slug.'" role="tab" data-toggle="tab">    
                        '.$category->name.'
                    </a>
                </li>';
        }
        echo '</ul>';
    
        echo '<div class="tab-content">';
        foreach($categories as $category) { 
            echo '<div class="tab-pane" id="' . $category->slug.'">';
            $the_query = new WP_Query(array(
                'post_type' => 'acme_product',
                'posts_per_page' => 100,
                'category_name' => $category->slug
            ));
    
            while ( $the_query->have_posts() ) : 
            $the_query->the_post();
            echo '<h1>';
                the_title();
            echo '</h1>';
            endwhile; 
        } 
        echo '</div>';
    ?>