TAGS :Viewed: 5 - Published at: a few seconds ago

[ Add a symbol before every single category inside a list display categories widget - Wordpress - WP_Widget_Categories ]

I want to add a symbol (the Hash symbol - #) before every category displayed inside the list display categoris widget in wordpress.

I am not adding the WP_Widget_Categories code to the post question because it is the very basic one that anyone will find with wordpress.

Anyway I am trying to make something like this:

Example

That's all. I started to study php from a few days and I don't know how to do this, any help will be really appriciated.

Answer 1


You can use a filter to modify the results generated by the default categories widget. Try adding this in your functions file:

function modify_widget_categories_listing( $content ) {
    return "#$content";
}
add_filter( 'list_cats', 'modify_widget_categories_listing', 10, 2 );

You may have to make some changes to what is being returned to get exactly what you're looking for, but that's the gist of it.

Answer 2


Put this in your functions.php

function show_categories_with_hash() {
    $terms = get_terms( 'categories' );
    echo '<ul>';
    foreach ( $terms as $term ) {
        $term_link = get_term_link( $term );
        if ( is_wp_error( $term_link ) ) {
            continue;
        }
        echo '<li><a href="' . esc_url( $term_link ) . '">#' . $term->name . '</a></li>';
    }
    echo '</ul>';
}