Thesis Tip #10 – Filter Categories from Homepage

by Bill Erickson on December 21, 2009

Post image for Thesis Tip #10 – Filter Categories from Homepage

You’ll often have post categories for things like “Events” that you don’t want showing up on your homepage with all your other posts. This is the easy way to filter them out.

Here’s what you should put in the custom_functions.php file:

/* Filter categories on the homepage */
function home_filter_begin() {
if(is_home()):
global $wp_query;
$exclude = '-32,-165,-103,-20,-23,-21,-22,-671';
query_posts(array_merge(array('cat' => $exclude ),$wp_query->query));
endif;
}
add_action('thesis_hook_before_content','home_filter_begin');

function home_filter_end() {
if(is_home()):
wp_reset_query();
endif;
}
add_action('thesis_hook_after_content','home_filter_end');

And here’s what it means:

  • function home_filter_begin() { – This is the name of the filter
  • if(is_home()): global $wp_query; – If this is the homepage, load the wordpress query
  • $exclude = '-32,-20'; – Create a variable called “exclude” that lists all the categories we’ll exclude
  • query_posts(array_merge(array('cat' => $exclude ),$wp_query->query)); – Do a new wordpress query, merging the existing one and the list of excluded categories
  • endif; } add_action('thesis_hook_before_content','home_filter_begin'); – End the if statement, and insert this function before the content.
  • function home_filter_end() { if(is_home()): wp_reset_query(); endif; } add_action('thesis_hook_after_content','home_filter_end'); – At the end of all the posts on the homepage, reset the wordpress query back to what it was.
All Thesis Tips:

Bill Erickson is a WordPress Consultant who builds custom websites using WordPress as a CMS and Thesis or Genesis as a framework. He’s a cofounder and resident of The Creative Space, and a cofounder of the BIL Conference (the open analog to the TED Conference).

{ 1 comment… read it below or add one }

Alex August 1, 2010 at 3:26 pm

Bill, thanks for this. Will this work for Thesis 1.7? Also would you have a tutorial that shows how to change the standard 2 column layout for teasers into a 3 or 4 column layout and to only show the post image rather than the exerpt with the post image.

Please shoot me an email if you do.

Reply

Leave a Comment

Previous post:

Next post: