0

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!

1 Answer 1

1

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);
1
  • 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
    Commented Jun 30, 2013 at 2:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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