• Hello,
    I need to know how to add a span tag to a post title. What I need to accomplish is to have the first word of each title be a different color then the remaining words. I know it’s possible as it is accomplished on this blog.

    http://harvesthealth5k.com/?page_id=164

    Could anyone help me? I would very much appreciate it.

    modifying

Viewing 4 replies - 1 through 4 (of 4 total)
  • for just the page title, edit page.php and replace the_title(); with:

    $words = explode(' ', the_title('', '',  false));
    $words[0] = '<span>'.$words[0].'</span>';
    $title = implode(' ', $words);
    echo $title;

    —-
    or for generally all titles (this will add the span around the first word in any title, such as post titles, page titles, and the_title is also used for instance in page lists) –
    add something like this to functions.php of your theme:

    add_filter('the_title', 'span_first_word');
    function span_first_word($title) {
    $words = explode(' ', $title);
    $words[0] = '<span>'.$words[0].'</span>';
    $title = implode(' ', $words);
    return $title;
    }
    Thread Starter swellness

    (@swellness)

    Oh my that worked like a charm! Thank you so much I thought I was going to have to stew over this for hours. I very much appreciate you time!

    Any way on how to target all instances of ‘the’ within a the_title();?

    Any feedback would be greatly appreciated, Thanks!

    Hi Sweeper,

    That is most excellent!! I have been looking for something like this!

    How would I restrict this to only work on Page and Post titles?

    Thanks!

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Adding Span to Title’ is closed to new replies.