Getting wordpress to print the post date in a title of a post

Jan 13th, 2011 | By | Category: Random Musings

Wow, getting wordpress to print a few bloody words in the title of a post turned out to be a massive ordeal here.  For one of my projects I have a post that uses php (thanks to php-exec for allowing php be to be run inside of a WP post) to pull rss feeds and populate the post body with it’s information.  So what I do with this is I just have wordpress repost the same post every day (thanks to the plugin ‘reposter reloaded) but what I want to happen is for the title of the post to be updated every day with the date of the repost.  So I can say something like ‘New info for January 13th 2010′ and the date in that title will be updated to January 14th 2010 when it gets reposted tomorrow and so on and so forth.

Weeellllllllll this proved to be way more complicated than expected.. I was hoping, as with the other stuff that I would just be able to insert something like ‘the_date();’ into the title box and have php-exec automatically take that and miraculously transform it into the date but oooh noo, for whatever reason php is not allowed to be executed in the title.  Sure I can execute it in the body of a post, the excerpt but the title is just a little too sacred for that.  This left me with the fun task of mucking around with the template however I only wanted the date tacked onto the end of titles for posts that were in a specific category, but to make things even more complicated I didn’t even want it to go with a certain category, what I needed was for it to apply only to posts in categories that contained a specific keyword.

So what I had to do was create this little script that would look at the category which this post belonged, check this category contained a certain word and if it did it would tack on the date to the end of the title for the post.  Sounds easy enough eh?  Well I imagine for someone with a modicum of php and wordpress hacking experience this would have been easy but I unfortunately have neither of those things.  So 15 hours later or so it’s finally working :)  And to save you the time here it is, over commented for others with similarly limited experience in these matters:

Open up /wp-content/themes/YOURTHEME/loop.php (you may also need to do the same in single.php but I’m not sure if it’s necessary)

<?php
// lets find out what category this post belongs to and apply that to a new variable

$category = get_the_category_list( ‘, ‘ );

// Lets do the same thing but with the date

$date = the_date();

// now we need to look at the $category variable and see if it contains a specific keyword, in this case the word is ‘job’

$pos = strpos($category, job);
// If it doesn’t contain this keyword then strpos will return a ‘false’ value, in which case we want it to append empty air to the end of the title

if ($pos === false) {
echo ” “;

// Now if the keyword is contained in our search parameters what strpos does is returns a numeric value indicatin the postition in our search terms where our keyword starts.  We dont’ really care about that, all we care about is that strpos is return a value, any value that isn’t false and if this is the case then we have our $date variable tacked echoed as seen below
} else    {
echo “$date” ;
}
?>

Now what I did is I placed all of this inside the <H1> title tags in the single.php file.  So normally it would look like this:

<h1><?php the_title(); ?> </h1>

But now it looks like this:

<h1><?php the_title(); ?>
<?php
$category = get_the_category_list( ‘, ‘ );
$date = the_date();
$pos = strpos($category, job);

if ($pos === false) {
echo ” “;
} else    {
echo “$date” ;
}
?>
</h1>

Tada…

Tags: , , , ,

Leave a Comment