Creating Date Based Design Elements In Thesis

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.

IMG_1852

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.

Sign Up Now for Free Updates and Exclusive Content:

Learn how to write killer content, get more traffic, make money, and more by entering your email below:

Written by Adam Baird

Adam is a Wordpress designer, Thesis specialist, and blogger from Indianapolis, Indiana. He writes web design tutorials and resources on Theme Big. Check out his custom work here.

Comments

  • kristarella says:

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

    • Adam Baird says:

      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.

      • kristarella says:

        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.

        • Adam Baird says:

          Fair enough. You could also add a p.alert to let people know that an article is out of date…

        • Nick Reese says:

          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.

          • Adam Baird says:

            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.

  • […] posts: Creating Date Based Design Elements in Thesis How to Use Thesis Custom Body Classes 301 Redirect Any Thesis […]