Nov
02
WordPress: Add HTML tags to first word of title
While working on a theme, I needed code to add span tags around the first word of my titles in order to style them differently. Below is the code to do it. Just copy it into your functions.php file.
function arixWP_title()
{
global $post;
$title = '<h2 class="title"><a href="' . get_permalink() . '" title="' . get_the_title() . '" rel="bookmark">' . get_the_title() . '</a></h2>';
$title = preg_replace('/<a([^>]+)>([A-z0-9_]+)\s/i', '<a$1><span>$2</span> ', $title);
echo $title;
}
Usage
Where ever you would like the title to show up paste the following code:
<?php arixWP_title(); ?>
Tags: post title, WordPress


