Nov

02

WordPress: Add HTML tags to first word of title

Posted by CodeMunkyX

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: ,

Socialize

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

 

featured wordpress themes