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