Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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>
share|improve this question

1 Answer 1

up vote 3 down vote accepted

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
share|improve this answer
    
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! –  songdogtech Nov 13 '11 at 17:40

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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