Return to Snippet

Revision: 15325
at March 22, 2010 00:57 by kristarella


Updated Code
<?php

// just before loop in category theme file:
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$current_cat = get_query_var('cat');
$args=array(
	'category__in' => array($current_cat),
	'paged' => $paged
);
query_posts($args);

// after loop in category theme file
$cat = get_query_var('category__in');
set_query_var("cat",$cat[0]);

// or in Thesis custom_functions.php
function remove_child_cats() {
if (is_category()) :
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $current_cat = get_query_var('cat'); 

    $args=array(
        'category__in' => array($current_cat),
        'paged' => $paged
    );
    
    query_posts($args);
    
    remove_action('thesis_hook_archive_info', 'thesis_default_archive_info');
        echo '            <div id="archive_info">' . "\n";
?>
                <p><?php _e('From the category archives:', 'thesis'); ?></p>
                <h1><?php echo get_cat_name($current_cat); ?></h1>
<?php
        echo '            </div>' . "\n";
endif;
}
add_action('thesis_hook_before_content','remove_child_cats');

function reset_cat() {
    $cat = get_query_var('category__in');
    set_query_var("cat",$cat[0]);
}
add_action('thesis_hook_after_content','reset_cat');

Revision: 15324
at March 22, 2010 00:56 by kristarella


Updated Code
// just before loop in category theme file:
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$current_cat = get_query_var('cat');
$args=array(
	'category__in' => array($current_cat),
	'paged' => $paged
);
query_posts($args);

// after loop in category theme file
$cat = get_query_var('category__in');
set_query_var("cat",$cat[0]);

// or in Thesis custom_functions.php
function remove_child_cats() {
if (is_category()) :
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $current_cat = get_query_var('cat'); 

    $args=array(
        'category__in' => array($current_cat),
        'paged' => $paged
    );
    
    query_posts($args);
    
    remove_action('thesis_hook_archive_info', 'thesis_default_archive_info');
        echo '            <div id="archive_info">' . "\n";
?>
                <p><?php _e('From the category archives:', 'thesis'); ?></p>
                <h1><?php echo get_cat_name($current_cat); ?></h1>
<?php
        echo '            </div>' . "\n";
endif;
}
add_action('thesis_hook_before_content','remove_child_cats');

function reset_cat() {
    $cat = get_query_var('category__in');
    set_query_var("cat",$cat[0]);
}
add_action('thesis_hook_after_content','reset_cat');

Revision: 15323
at March 21, 2010 18:22 by kristarella


Updated Code
// just before loop in theme file:
if (is_category()) :
	$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
	$current_cat = get_query_var('cat');
	$args=array(
	  'category__in' => array($current_cat),
	  'paged' => $paged
	);
	query_posts($args);
endif;

// or in Thesis custom_functions.php
function remove_child_cats() {
if (is_category()) :
	$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
	$current_cat = get_query_var('cat');
	$args=array(
	  'category__in' => array($current_cat),
	  'paged' => $paged
	);
	query_posts($args);
endif;
}
add_action('thesis_hook_before_content','remove_child_cats');

Revision: 15322
at June 30, 2009 22:29 by kristarella


Initial Code
function exclude_child_cats($query) {
	if ( is_category() ) :
		$current_cat = single_cat_title('',false);
		$current_cat_ID = get_cat_ID("$current_cat");
		$ex_cats = get_categories("child_of=$current_cat_ID");
		
		foreach ($ex_cats as $ex_cat) :
			$excluded[] = $ex_cat->cat_ID;
		endforeach;
		
		$ex = '-' . implode(' -', $excluded);
      	$query->set('cat', "$current_cat_ID . $ex");
      	
		return $query;
	endif;
}
add_filter('pre_get_posts', 'exclude_child_cats');

Initial URL

                                

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

Initial Title
Exclude child categories from category archive

Initial Tags
wordpress

Initial Language
PHP