We Recommend

Wicked Cool PHP: Real-World Scripts That Solve Difficult Problems Wicked Cool PHP: Real-World Scripts That Solve Difficult Problems
Wicked Cool PHP contains a wide variety of scripts to process credit cards, check the validity of email addresses, template HTML, and serve dynamic images and text.


Posted By

kristarella on 06/30/09


Tagged

wordpress wp


Versions (?)


Who likes this?

2 people have marked this snippet as a favorite

tobiasmay
joelhaus


Exclude child categories from category archive


Published in: PHP 


  1. function exclude_child_cats($query) {
  2. if ( is_category() ) :
  3. $current_cat = single_cat_title('',false);
  4. $current_cat_ID = get_cat_ID("$current_cat");
  5. $ex_cats = get_categories("child_of=$current_cat_ID");
  6.  
  7. foreach ($ex_cats as $ex_cat) :
  8. $excluded[] = $ex_cat->cat_ID;
  9. endforeach;
  10.  
  11. $ex = '-' . implode(' -', $excluded);
  12. $query->set('cat', "$current_cat_ID . $ex");
  13.  
  14. return $query;
  15. endif;
  16. }
  17. add_filter('pre_get_posts', 'exclude_child_cats');

Report this snippet 

You need to login to post a comment.