ramin
ramin

Reputation: 480

remove single_cat_title from wp_list_categories

how to remove or(exclude) current page category(single_cat_title) from wp_list_categories?

I want to remove the current page category from my category list

<?php 
    $cat = single_cat_title( '', false );
    function text_replace( $output ) {
        $output = str_replace( '$cat', '', $output );
        return $output;
    }

    add_filter('wp_list_categories', 'text_replace'); ?>

<?php 
    wp_list_categories( array(
        'child_of'            => 1208,
        'current_category'    => 0,
        'depth'               => 0,
        'echo'                => 1,
        'hide_empty'          => 1,
        'hide_title_if_empty' => false,
        'hierarchical'        => true,
        'order'               => 'DESC',
        'orderby'             => 'count',
        'show_count'          => 0,
        'show_option_none'    => __( 'No categories' ),
        'style'               => 'list',
        'taxonomy'            => 'category',
        'title_li'            => 0,
        'use_desc_for_title'  => 0,

    ) ); 
?> 

Upvotes: 0

Views: 219

Answers (2)

ramin
ramin

Reputation: 480

fix the problem :

<?php 
function text_replace($output) {
$current_category = single_cat_title("", false); 
$cat = array("$current_category");
$output = str_replace($cat, '', $output);
return $output;
}
add_filter('wp_list_categories', 'text_replace');
?>
<?php wp_list_categories( array(
    'child_of'            => 1208,
    'current_category'    => 0,
    'depth'               => 0,
    'echo'                => 1,
    'hide_empty'          => 1,
    'hide_title_if_empty' => false,
    'hierarchical'        => true,
    'order'               => 'DESC',
    'orderby'             => 'count',
    'show_count'          => 0,
    'show_option_none'    => __( 'No categories' ),
    'style'               => 'list',
    'taxonomy'            => 'category',
    'title_li'            => 0,
    'use_desc_for_title'  => 0,

) ); 

?> 

Upvotes: 0

DubVader
DubVader

Reputation: 1072

You can use the 'exclude' parameter to exclude certain categories. You could get the terms for the post, put them in an array, and pass that to your wp_list_categories query.

<?php 


$terms_to_exlude = array();
$terms = get_the_terms( get_the_ID(), 'category' );

if ($terms) { 

   foreach ($terms as $term) {

      $terms_to_exclude[] = $term->term_id;

   }

} else {

  $terms_to_exclude = '';

}

  wp_list_categories( array(
        'child_of'            => 1208,
        'current_category'    => 0,
        'depth'               => 0,
        'echo'                => 1,
        'exclude'             => $terms_to_exclude,
        'hide_empty'          => 1,
        'hide_title_if_empty' => false,
        'hierarchical'        => true,
        'order'               => 'DESC',
        'orderby'             => 'count',
        'show_count'          => 0,
        'show_option_none'    => __( 'No categories' ),
        'style'               => 'list',
        'taxonomy'            => 'category',
        'title_li'            => 0,
        'use_desc_for_title'  => 0,

    ) ); 

?> 

Upvotes: 1

Related Questions