Summary
Using date based conditional statements it is entirely possible to display different content before and after posts depending on the age of the post. This is a little-known technique that can really open up the possibilities in terms of having greater control over where your visitors go when they visit your blog.
The Code
Let’s run through the basics of setting up date based content. Once you’ve got this, we’ll go a bit more in depth.
function date_based_function() {
$post_age = ( current_time(timestamp) - get_the_time('U') ) / (24*60*60);
if($post_age < 30) {
// your code here
}
}
add_action('thesis_hook_of_your_choice', 'date_based_function');
The first thing we do is determine how old a particular post is. $post_age = ( current_time(timestamp) - get_the_time('U') ) / (24*60*60); is the line that determines how old the post is in terms of days.
current_time(timestamp) is exactly what it sounds like – it determines the current time. get_the_time('U') represents the time that the post was written. Both of these dates are measured to the second. Thus, when you subtract one from the other you come up with an amount of time that is measered down to the second.
With that in mind, we need to determine the number of seconds in a day in order to determine how old the post is in terms of days. In order to do this we use (24*60*60) to represent one day. We set up the simple division and end up with $post_age representing the number of days since the post was published.
From that point its very simple. We use a simple conditional (if($post_age < 30) {}) that determines whether or not the post is less than 30 days old and displays the contents of that conditional if the post is, in fact, less than 30 days old. You can also replace < (less than) with > (greater than), <= (less than or equal to), >= (greater than or equal to), or == (equal to).
The last thing we need to do is use a Thesis hook to place the element where we want it. You can find a list of available hooks here.
Practical Application
Fresh content is inherently more credible than older content. This is a bit dependent upon your particular niche, but generally its a good idea to display the date for the first month or so after a post is published. However, you do not want to display the date for older posts where it would hurt your credibility.
Think about it. Are you more likely to read content from 2010 or from 2004?
Let's set the date to display only for the first 30 days after a post is published. Here is the code:
function byline_date() {
$post_age = ( current_time(timestamp) - get_the_time('U') ) / (24*60*60);
if($post_age <= 30) {
the_time('n - j - Y');
}
}
add_action('thesis_hook_byline_item', 'byline_date');
This is pretty simple. We just use the basic function and specify that if the post is less than or equal to 30 days old, we display the date. The date is represented by the_time('n - j - Y'). To learn how to customize this function to display the date any way you like check out the WP codex.
There are plenty of other possibilities. Using your creativity and date based hooks, you can have a large impact on the page views, monetization, and many other aspects of your site.
Get Free Updates From Art Of Blog
Sign up to receive insider tips only available to our email subscribers.
You can also subscribe to our RSS feed or follow us on Twitter to get the latest updates. Check out our YouTube channel for the latest video tutorials!





Thesis 1.8 and Genesis 1.3 Compared
Two Sure-Fire Ways to Increase Website Traffic
Thesis Recent Tweets with @Anywhere Integration
Add A Tweet This Post Box To Your Thesis Post Footer
WordPress Security 101: Securing your Site
Interview with Chris Pearson of DIYThemes
Enable Your Fans – Top Community Building and Social Sharing Tools for Your Blog
Using WordPress Pages like a Pro
180+ Thesis Design Customizations You have to See to Believe
Thesis WordPress Theme Review
{ 6 comments… read them below or add one }
“However, you do not want to display the date for older posts where it would hurt your credibility.
Think about it. Are you more likely to read content from 2010 or from 2004?”
I would wonder if it’s still relevant since 2004, but what if the post isn’t relevant anymore? How much will it hurt your credibility to have people reading stuff that is not true anymore (but was at the time) and not give them an easy way to see that the post is old?
Its a fair point. Of course its good practice to update your posts to replace irrelevant or inaccurate information. If you’re doing that then this shouldn’t be an issue.
I do some level of updating when it’s necessary, whether it be referring people to a new post, or adding in another line of CSS etc, but overall I still use my site as a blog, i.e., a “web log”. There’s a certain inherit timeliness about it. If someone were completely rewriting old posts, or deleting them to make sure the site is 100% up to date, not showing the date would be fine.
Fair enough. You could also add a
p.alertto let people know that an article is out of date…I think showing the date or not is completely up to the author’s discretion. Not showing it will increase the amount of time a visitor spends on your site. However there are some sites, such as news sites, that should have the date.
Personally I prefer showing it on the archives and home page, but on the single posts leaving it out.
If someone is real ambitious they will see the dates of the comments.
I do agree that some sites need the date and some sites need to avoid it.
It is worth noting that with Thesis 1.7 or later you can choose not to display the date with comments.
{ 1 trackback }