How do I reset a query that generates a list of categories with wp_list_categories?
The query below builds a two column list of categories that is then displayed with the second code chunk.
I’m using two of these queries (with different category include strings) in a jQuery tab to display different lists of categories.
Problem is, the first query needs to be reset, because the second tab shows both lists of categories rather than categories from just the second query.
I’ve tried <?php wp_reset_query();?>
but that only works for the WP loop. Anyone have any ideas?
<?php
$cats = explode
("<br />",wp_list_categories('title_li=&echo=0&depth=1&style=none&include=12,13,14,15,16'));
$cat_n = count($cats) - 1;
for ($i=0;$i<$cat_n;$i++):
if ($i<$cat_n/2):
$cat_left = $cat_left.'<li>'.$cats[$i].'</li>';
elseif ($i>=$cat_n/2):
$cat_right = $cat_right.'<li>'.$cats[$i].'</li>';
endif; endfor; ?>
<ul class="leftcatcolumn">
<?php echo $cat_left;?>
</ul>
<ul class="rightcatcolumn">
<?php echo $cat_right;?>
</ul>
wp_list_categories()
is a simple function that returns (or echos, depending on parameter) a list of links to category archives. It needs not to be reset.When you create a new instance of the
WP_Query
class you need to reset postdata, because Template Tags otherwise use that query and not the main query.You’ve said:
I am assuming, the “category include strings” are the only difference in the two queries?!
In the second time you’re using this snippet, the variables
$cat_left
and$cat_right
are the actual culprits. They do not get overwritten, the new values are added to the end of the array.Rename them to
$cat_left_2
and$cat_right_2
the second time around or set them toNULL
inbetween: