Search code examples
phpwordpress

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,

    ) ); 
?> 

Solution

  • 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,
    
    ) ); 
    
    ?>