WordPress function to style the first word of a post

posted in Wordpress

WordPress function to style the first word of a post

So I was looking for a way to style the first word of my title in wordpress as I wanted to make it italic. Why the hell did you want to do that? Its a style thing and just because I wanted to (that’s reason enough isn’t it?). I did find something that was close to what I wanted on google (http://designedbysarah.com). Not quite what I wanted, it wasn’t a function (no big deal).

So I set about to convert it – I adapted it to make my own function that I could pop into functions.php.
Now I’m not a advanced php programmer, hell I normally just google everything, and modify what I find to suit my needs – why re-invent the wheel? So this code is quite quick and dirty, I’m sure others could write it better, leaner, and much meaner – but it works.

You can see the code in action in every post title on this blog. All I have done is added the function below to my functions.php file, and replaced the wordpress the_title(); tag to my function of mypagetitle(); just change it in any template that you want to style the first word.

So if you are looking to replace the first word of your title tag, feel free to copy and amend the code below to suit your needs. I have used the span tag and added css for that but you could change it to anything you wanted to, you could change the span for em, anything that takes your fancy.

// function to make first word italic by using span
function mypagetitle() $space_pos+strlen($before)

p.s. I would like to thank designedbysarah for her post, without her code it would have taken me much longer to get mine up and running. Thank you.

 

Sign up

Like what your read? Join other designers subscribe to my irregular newsletter.


Your Email (required)

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>