Adding a <span> to widget title in WordPress

I am trying to change the color of the first word in the widget title in wordpress. I am using WP v.3.7.1 and a custom child theme that I created off of the twentythirteen theme.

I just need to wrap the first word in a and can style it from there. I tried to add the following code to the function.php but it only works for one of the three widgets that I have. I have tried other widgets and it doesn’t work either.

Read More
add_filter ('widget_title', 'wpzoom_fix_widgets');

function wpzoom_fix_widgets($old_title) {

    $title = explode(" ", $old_title,2);

    $titleNew = "<span>$title[0]</span> $title[1]";

    return $titleNew;

}

Any suggestions?

Thank you!

Related posts

Leave a Reply

1 comment

  1. Take a look at the first answer on WordPress Answsers, it looks close to what you are looking for:

    add_filter('widget_title', my_title);
    
    function my_title($title) {
        // Cut the title to 2 parts
        $title_parts = explode(' ', $title, 2);
    
        // Throw first word inside a span
        $title = '<span class="my_class">'.$title_parts[0].'</span>';
    
        // Add the remaining words if any
        if(isset($title_parts[1]))
            $title .= ' '.$title_parts[1];
    
        return $title;
    }
    

    Good luck 🙂