1

With the following code I am showing a list of sub categories:

<ul class="subcats-blog">     
    <?php
        if ( is_category() ) {
            $current_cat = get_query_var('cat');
                    wp_list_categories('&title_li=&child_of='.$current_cat);
        }
    ?>
</ul>

What I am trying to do is to strip the word 'news' from any list item. I figured that I can do it by the use of preg_replace, but the problem is my knowledge of php is very limited, and I'm not able to glue the two together. Any help would be greatly appreciated.

1 Answer 1

1

I assume you want to replace the "news" string in the category names and not in the category links.

The default category walker contains this line:

$cat_name = apply_filters( 'list_cats', $cat_name, $category );

that allows you to modify the category names that will be displayed.

So you could try (untested):

<ul class="subcats-blog">     
    <?php
        if ( is_category() ) {
            $current_cat = get_query_var('cat');

            // Add your custom replace filter:
            add_filter( 'list_cats', 'wpse_155534_replace' );

            wp_list_categories('&title_li=&child_of='.$current_cat);

            // Remove your custom replace filter:
            remove_filter( 'list_cats', 'wpse_155534_replace' );
        }
    ?>
</ul>

where

/**
 * Replace the "news" string in the category names
 *
 * @see http://wordpress.stackexchange.com/a/155539/26350
 *
 * @param  string $cat_name
 * @return string $cat_name
 */
function wpse_155534_replace( $cat_name )
{
    return str_ireplace( 'news', '', $cat_name );
}
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.