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.

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.

Adam has a decade of experience as a WordPress designer and developer. Learn more about him here.

Written by

Adam has a decade of experience as a WordPress designer and developer. Learn more about him here.


Comments are closed.