Exclude child categories from category archive


/ Published in: PHP
Save to your folder(s)

Completely changed original function, which stopped working around WP2.8.


Copy this code and paste it in your HTML
  1. <?php
  2.  
  3. // just before loop in category theme file:
  4. $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
  5. $current_cat = get_query_var('cat');
  6. $args=array(
  7. 'category__in' => array($current_cat),
  8. 'paged' => $paged
  9. );
  10. query_posts($args);
  11.  
  12. // after loop in category theme file
  13. $cat = get_query_var('category__in');
  14. set_query_var("cat",$cat[0]);
  15.  
  16. // or in Thesis custom_functions.php
  17. function remove_child_cats() {
  18. if (is_category()) :
  19. $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
  20. $current_cat = get_query_var('cat');
  21.  
  22. $args=array(
  23. 'category__in' => array($current_cat),
  24. 'paged' => $paged
  25. );
  26.  
  27. query_posts($args);
  28.  
  29. remove_action('thesis_hook_archive_info', 'thesis_default_archive_info');
  30. echo ' <div id="archive_info">' . "\n";
  31. ?>
  32. <p><?php _e('From the category archives:', 'thesis'); ?></p>
  33. <h1><?php echo get_cat_name($current_cat); ?></h1>
  34. <?php
  35. echo ' </div>' . "\n";
  36. endif;
  37. }
  38. add_action('thesis_hook_before_content','remove_child_cats');
  39.  
  40. function reset_cat() {
  41. $cat = get_query_var('category__in');
  42. set_query_var("cat",$cat[0]);
  43. }
  44. add_action('thesis_hook_after_content','reset_cat');

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.