2

I am trying to generate a dynamic tab based navigating with query post my code is like this

            <div class="tabbable">
            <ul class="nav nav-tabs">
            <?php
            $my_query = new WP_Query(array(
            'showposts' => 5,
            'orderby' => 'rand',
            'category_name' => 'Recipes'
            ));
            $x = 0;
            while ($my_query->have_posts()) : $my_query->the_post(); $x++; 
            $category = get_the_category(); 
            ?>          
            <?php if ($x == 1) { echo '<li class="active">'; } else {echo '<li>';} ?>
            <a href="#tab<?php echo $x; ?>" data-toggle="tab"><?php echo $category[0]->cat_name; ?></a></li>
            <?php endwhile;?>     
            </ul>

            <div class="tab-content">
            <div class="tab-pane active" id="tab1">
              <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. .</p>
            </div>
            <div class="tab-pane" id="tab2">
              <p>Ut consectetur libero nec nulla ullamcorper lobortis pulvinar libero viverra.</p>
            </div>
            <div class="tab-pane" id="tab3">
              <p>Vivamus eget velit at mauris blandit consequat vel sed ipsum.</p>
            </div>
            </div>
            </div>

this is all ok and working fine, the problem is that above code can repeat any specific category. for example it can show

cat1     cat2     cat3    cat1   cat2

this is what I dont want, I need any category not duplicating again. how to over come this problem?

just to keep in mind that in second stage I will be using rewind_post() to get the data from above categories so I want to keep the loop inline

Thank you

ok I have update my code as following

            <div class="tabbable">
            <ul class="nav nav-tabs">
            <?php
            $my_query = new WP_Query(array(
            'showposts' => 3,
            'orderby' => 'rand',
            'category_name' => 'Recipes'
            ));
            while ($my_query->have_posts()) : $my_query->the_post();
            $x = $my_query->current_post + 1;  //use this to count instead
            $categories = get_the_category();
            foreach($categories as $category) {
            if ($x == 1) { echo '<li class="active">'; } else {echo '<li>';} ?>
            <a href="#tab<?php echo $x; ?>" data-toggle="tab"><?php echo $category->cat_name; ?></a></li>
            <?php
            }
            endwhile;
            ?>  
            </ul>

now, how do I show 4 recipes from each categories in the tab contents section

1 Answer 1

1

You need to do a foreach for the get_the_category

Something like:

while ($my_query->have_posts()) : $my_query->the_post();

$x = $my_query->current_post + 1;  //you can use this to count instead
$categories = get_the_category();

foreach($categories as $category) {
 if ($x == 1) ...
//rest of your output
// remember to us $category->cat_name; instead of what you have above
}
2
  • Sorry by mistake I update the code in wrong section, please see my comment and revised code above Commented Jul 28, 2012 at 13:39
  • On WSPE you should ask one question at a time, it's not a forum. That way it will benefit other users. To answer the second added question you need to use rewind_posts:codex.wordpress.org/Function_Reference/rewind_posts
    – Wyck
    Commented Jul 28, 2012 at 23:53

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.