Thesis Tutorial – Adding Date Based Triggers to Your Posts

Post image for Thesis Tutorial – Adding Date Based Triggers to Your Posts
Michael Gray

By Michael Gray
In Blogs  

Print Post Print Post Email Post Email Post    ADD TO STUMBLEUPON Sphinn It ADD TO DEL.ICIO.US  Tweet This

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

Related posts:

  1. Thesis Tutorial – How to Add Adsense Section Targeting Using Adsense on your blog usually isn’t the most profitable...
  2. Make Thesis Work Better With Digg and Facebook If you’re involved with social media sites like Digg, Facebbok...
  3. How to Add a Carousel to Your Thesis Blog If you’ve spent any time visiting blogs lately chances are...

Crazyegg Link Tracking

{ 14 comments }

BuildAndEarn March 4, 2009 at 7:14 am

Very nice tutorial. I recently wrote an article about putting ads on old posts and suggested the practice should be MUCH more popular/widespread and gave them the name Contingency Ads.

I’m eager to start exploring some contingencies beyond “date” based ones and your example of social media icons on new posts has me thinking that “date” isn’t the only dimension I should be exploring. Thanks for putting my mind in motion!

chuckallied March 4, 2009 at 11:21 am

I’ve yet to poke my nose into Thesis although I have been creating some Wordpress blogs lately and get how this can be useful. How do you get around the date issue in the SERPs though? Erase the date, slightly rename, and re-submit the post? Or is it just as simple as not having a date on the page?

Michael Gray March 4, 2009 at 11:29 am

I will take the date and put it in a different spot based on how old the post is, when it’s new it’s at the top in the by line, when it’s old I’ll drop it to the end of the post

netmeg March 4, 2009 at 11:25 am

Very useful – thanks!

PerfectMOney March 4, 2009 at 11:38 am

OH boy you really got the language,Michael and honestly I dont know what technical issue you thought with your debug poetry,however in normal language ,I believe I need to bookmark this post and tell my webmaster that I need to improve my blog with this code.once again thanks the tutorial

chuckallied March 4, 2009 at 12:04 pm

Does moving the date around remove Google’s date stamp in the SERPs?

Michael Gray March 4, 2009 at 12:07 pm

@chuckallied doesn’t have an effect on rankings but sometimes people wont click through on a post if it’s “old”

Bruce Keener March 4, 2009 at 9:13 pm

Mighty fine post, sir. Thanks for the coding. I’ll put it to use.

seo wales March 5, 2009 at 12:47 am

I just started using jQuery (which is available in WP by default apparently) and my first bit of code with it was to check the height of my content area and only display a skyscraper if there was room without increasing the height of the page, it’s in action at http://alicious.com . That’s a type of contingency, contingent on space being available. Perhaps I should write it up.

Michael Gray March 5, 2009 at 8:02 am

@seo wales as I understand it jQuery is client side code, using client side code for mission critical functions is a plan for disaster. I’m “old” and a I grew up when browser compatibility was really an issue, and learned that the hard way. Server side code is never sexy like JS or Ajax, but it is dependable. So be careful.

Femin March 5, 2009 at 3:18 am

Nice Tip.Thanks

Chung Bey Luen March 6, 2009 at 12:53 pm

Great tips. Thanks. I am using Thesis Theme and I find it easier to customize with Thesis.

Ian M March 11, 2009 at 9:23 am

“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”

gah! caught out!

James Mann March 12, 2009 at 5:26 am

Great tutorial.
I like how just adding some simple code, can automatically allow your site to be ever changing without having to open files, manually change things and then re-upload. BTW, Does anyone know anything about these SEO Wordpress plugins, i heard about?

Comments on this entry are closed.