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.

I'm trying to exclude categories from a function-based category list. I browsed and researched here a lot, but couldn't find a solution. Here's the code I use in my sidebar:

 <ul class="side-cat-img"> <li> 
<?php $args = array();
$args['show_last_updated'] = true;
$args['show_count'] = true;
$args['title_li'] = '';
$args['walker'] = new CategoryThumbnail_Walker;

wp_list_categories($args); ?>
</li>  
</ul> 

I want to exclude a few categories, so I used this code:

<?php wp_list_categories('exclude=4,7&title_li='); ?>

Result: The categories were really excluded, but the function didn't work properly anymore.

The function is supposed to pull out a screencap of each category to display. Once I excluded categories, the screenshots didn't show up anymore.

Here's the function code:

class CategoryThumbnail_Walker extends Walker_Category {

    // A new element has been stumbled upon and has ended
    function end_el( &$output, $category, $depth, $args ) {
        // Output the standard link ending
        parent::end_el( &$output, $category, $depth, $args );

        // Get one post
        $posts = get_posts( array(
            // ...from this category
            'category' => $category->cat_ID,
            'numberposts' => 10
        ) );

        // we'll record the seen images here
        if ( !isset($this->images_seen) ) $this->images_seen = array();

        foreach ( $posts as $post ) {
            // Get its thumbnail and append it to the output
            $featured = get_the_post_thumbnail( $post->ID, 'latestpost-thumb', null );
            // have we already seen this image?
            if ( in_array($featured, $this->images_seen) ) continue;
            else {
                $this->images_seen []= $featured;
 $post_cats = get_the_category($post->ID);
$output .= '<li><a href="http://zoomingjapan.com/category/'.$post_cats[0]->slug.'" title="Category Name">'.$featured.'</a></li>'; 
                break;
            }
        }
    }
}

Any ideas how I can change the code to exclude categories without destroying what the function is supposed to do?

Thanks a lot in advance!

share|improve this question

1 Answer 1

up vote 1 down vote accepted

This...

<?php wp_list_categories('exclude=4,7&title_li='); ?>

... does not include your Walker. Why didn't you just add that exclude the same way you added the others?

$args = array();
$args['show_last_updated'] = true;
$args['show_count'] = true;
$args['title_li'] = '';
$args['exclude'] = '4,7';
$args['walker'] = new CategoryThumbnail_Walker;

wp_list_categories($args);
share|improve this answer
    
Thank you. That worked. You have a small syntax error in there and forgot to close the "[". I didn't write the code, so I had no idea how to modify it. Thanks again! :) –  japanworm Jun 30 '13 at 2:55

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.