Thesis Tutorial – Adding Date Based Triggers to Your Posts

There are a lot of times when you are working on your blog template where you want things to happen based on how old a post is. This tutorial shows you how to do it. The programming is pretty simple and you don’t have to be using the WordPress Thesis Theme to do it, but it would be a lot easier if you were …

Lets tackle the big question first why would you want to change your blog based on how old each post is? When your posts are new maybe you want social media bookmarking buttons to appear, but you don’t want them on older posts. Maybe you don’t want to annoy your regular readers with advertising, but after a posts is over 30 days old you want to monetize your archives, with adsense. Maybe your listings in the SERP’s show a date with them, this may be helpful for posts that are new, but as posts age this may cut down on the number of clicks you get from a search engine. Those are just a few examples, there are many many more.

In this tutorial I’m going to show you how to ad a block of adsense under the author by line. Let’s step through the code, the first thing we want to do is figure out how old the post is, here’s how we do it:

//how many days old is this post
$daysold = (current_time(timestamp) - get_the_time('U') -
(get_settings('gmt_offset')))/(24*60*60);

That’s a lot of wordpress functions strung together that says, get the current time adjust to your local timezone and convert it to days (that’s the 24 hours times 60 minutes times 60 hours part). The variable $daysold now has a numerical value for how old the current post is.

Next we want to tell the code to only activate if it’s a single post

if (is_single()){
//code to do stuff goes here
}

This code uses a wordpress function that says if this is a single post do what’s inside the curly braces.  There are other wordpress functions like is_home() (for the homepage) and is_category() for category pages.

The last piece of code uses the $daysold value to test if the current page is older than our 14 day threshold and looks like this

if ($daysold > 14 ) {
//code to do stuff goes here
}

This code says if our post is older than 14 days do what’s inside of the curly braces. The last thing you need to know is this

<? php

and

?>

Since we’re working in PHP we need to tell the program when we want it to switch to plain HTML. The top one says stop doing HTML and start doing PHP the bottom one says stop doing PHP and start doing HTML. The file we’re working in is a PHP file so usually you’ll have to tell it to stop doing PHP and start doing HTML first.Now let’s put it all together.

If you aren’t already on thesis one of the things that makes it so great is all the custom programming goes in one file custom_functions.php that means no hunting around through template to make sure you have the right one or miss something, it’s just easier. We’re going to put out code inside of a function named uauthor_byline() here’s what it looks like:

//this appears under the author byline
function uauthor_byline() {
//how many days old is this post
$daysold = (current_time(timestamp) - get_the_time('U') -
(get_settings('gmt_offset')))/(24*60*60);
//only display on single posts
if (is_single()){ 
//only display  if post > 14 days old
if ($daysold > 14 ) { ?>
<script type="text/javascript"><!--
google_ad_client = "pub-xxxxxxxxxxxxx;
google_ad_width = 468;
google_ad_height = 60;
google_ad_format = "468x60_as";
google_ad_type = "text";
google_ad_channel ="xxxxxxx";
google_color_border = "FFFFFF";
google_color_bg = "FFFFFF";
google_color_link = "000000";
google_color_url = "000000";
google_color_text = "000000";
//--></script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
<? php
}
}
}

lets review we named a function uauthor_byline inside of that function we figured out how old the current post was. We said if this is a single post and if it’s over 14 days old go ahead and display an adsense banner. If you are going to copy and paste this code be sure to put in your adsense publisher ID and adsense channel.

Next we need to activate the code and tell thesis where to put it, that’s the easy part just one line of code with a thesis hook as follows

add_action('thesis_hook_before_post','uauthor_byline' );

If you are going to use this for adsense I’d also suggest using section targetting (see How to Add Adsense Section Targeting).

As I said in the begining of the article this code could be adapted to work with any wordpress template, it’s just a little messier than working with thesis. Also you don’t need to use it just for adsense you could use it for anything else like banner advertisement.

BONUS TIP

I like to mix things up on my blog, when posts are new they have social bookmarking buttons, when they are old they shift to adsense. There’s an easy way to do that with some slight changes to the code above. To do it you’ll need to take this code:

<? php
}
}
}

and change it to this code:

<? php
}else{
?>
<!-- the code for bookmarking buttons goes here-->
<?
}
}
}

What I just added was an else condition so if the post “fails” the older than 14 days old test then it does the other thing, in this case displays a bookmarking button.

I’m sure some nit-picky programmer type will be along to tell me it’s more efficient if I put the $daysold math inside of the is_single test, and if that were the only place it was used, yes I agree. However I’m using it in the same function to do other things, which is why I placed outside of the if-then condition.

I’m a big fan of thesis and have a few more tutorials, planned, if there’s something specific you’d like to see covered let me know in the comments below.

If you found this post helpful and you’re thinking of buying thesis using my affiliate link would make me happy. If you want to buy thesis directly that’s ok too … I’ll just be sad for weeks on end 🙁

GraywolfSEO.com runs on the Genesis Framework

Genesis Framework

Genesis lets you to quickly and easily build amazing websites with WordPress. Whether you're a novice or advanced developer, Genesis provides the secure and search-engine-optimized foundation that takes WordPress to places you never thought it could go.
It's that simple - start using Genesis now!


Take advantage of the 6 default layout options, comprehensive SEO settings, rock-solid security, flexible theme options, cool custom widgets, custom design hooks, and a huge selection of child themes ("skins") that make your site look the way you want it to. With automatic theme updates and world-class support included, Genesis is the smart choice for your WordPress website or blog.


tla starter kit

Advertisers:

  1. Text Link Ads - New customers can get $100 in free text links.
  2. BOTW.org - Get a premier listing in the internet's oldest directory.
  3. Need an SEO Audit for your website, look at my SEO Consulting Services
  4. TigerTech - Great Web Hosting service at a great price.