简体   繁体   中英

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.

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?

'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
)); 

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>';
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM