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?

    $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 class="rightcatcolumn">
    <?php echo $cat_right;?>

1 Answer 1


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'm using two of these queries (with different category include strings)...

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_rightare 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 to NULL inbetween:

// first list here
$cat_left = NULL;
$cat_right = NULL;
// second list here
  • You're right; I was looking at the wrong aspects of the query. I changed the second set of cat calls to $cat_left_1 and 2 and that worked. Thanks! Nov 13, 2011 at 17:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.