Hide Dates in Old WordPress Posts using Thesis Theme

by Ashwin

in Thesis Theme,Wordpress

thesis-hide-old-posts

Hiding the published-on date in old WordPress posts is requested in many Thesis Customizations.  Sometimes this helps in avoiding the content to appear outdated, for the readers of your site.

In this post, I will show you how to hide the date in posts using the Thesis Theme custom files, without the need to tweak any of the core files.  To know how – read on!

What we will do at the end of this tutorial?

We will try to do the following at the end of this very simple tutorial:

  • Hide Published-on date in the Post Bylines for posts older than 30 days
  • Also Hide these dates in Teasers – if the site is using any

Disable the Date Display in Thesis Options

Since we are coming up with our own logic to display the Dates, let us first disable the default date display in Thesis Theme.

To do this, go to WP Admin –> Thesis Options –> Display Options –> Bylines and un-check the “Show published-on date in post byline” option.  (Don’t for to click the Big Ass Save Button !)

Now the dates magically disappear on the post bylines!

Hide Dates in Posts older than 30 days

Now on to the job! We will have to display the dates only in posts that were written in the last 30 days and hide them on older posts.  To do this:

  • Calculate the age of the current post – in number of days
  • Display the date only if the posts was written in the last 30 days

The following code will help you calculate the age:

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

To add this date to the Byline item, we would need to override the ‘thesis_hook_byline_item’.  Following is the code does that will be added to the – /custom/custom_functions.php file.

function add_date_to_byline() {
  //Number of days this post is old
  $daysold = (current_time(timestamp) - get_the_time('U') -
(get_settings('gmt_offset')))/(24*60*60);

  if ($daysold ' . get_the_time(get_option('date_format')) . '';
  }
}

add_action('thesis_hook_byline_item', 'add_date_to_byline');

This code, once added will hide dates in all posts older than 30 days.

Oh, we forgot the Teasers!

First we must hide the Date in Teasers, to make sure that the default behavior gets out of the way. 

To do this, go to WP Admin –> Design Options –> Teasers –> Teaser Display Options and un-check the “date” option.  (Don’t for to click the Big Ass Save Button !)

Now add the following code to your custom_functions.php and this will hide dates from posts older than 30 days, on teasers.

function add_date_to_teaser() {
  //Number of days this post is old
  $daysold = (current_time(timestamp) - get_the_time('U') -
(get_settings('gmt_offset')))/(24*60*60);

        global $thesis_design;
        $date_formats = thesis_get_date_formats();
        $use_format = ($thesis_design['teasers']['date']['format'] == 'custom') ? $thesis_design['teasers']['date']['custom'] : $date_formats[$thesis_design['teasers']['date']['format']];

  if ($daysold ' . "\n";
  }
}

add_action('thesis_hook_after_teaser_headline','add_date_to_teaser');

Note we are using the thesis_hook_after_teaser_headline hook for getting things done.  Now we are truly done!

Credits

Following posts were useful in obtaining handy information for writing this tutorial.

  • Removing the Date from Older Posts in WordPress
  • Adding Date based Triggers to your Posts

Hope you found this post useful.  If yes, please share it using Twitter and Facebook, for more readers to benefit

  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  • Ricky Henderson
    Thanks for posting this Ashwin. Is there code for Thesis that can also hide the dates of the comments on older posts?
  • Ashwin
    Hi Ricky,

    If you are using a plugin for comments - like Disqus etc. it is not possible.
    However if native comments are being used, then it can be possible using the hooks and code tweaks.
  • Ricky Henderson
    Thanks Ashwin. I am using the default Wordpress commenting system, however I don't have a lot experience with Thesis hooks. Could you provide a sample of code to hide old comments' dates and times similar to what you did with post dates? I would greatly appreciate it.
  • Jan
    Ashwin,
    Great post! This may be a little different subject from your post -- trying to make the date "Jun 16" with a couple of background colors -- one small block and another bigger block -- by the headline. For example,
    it would look like this:
    Jun 16 2nd bg color> color> Headline title here

    Not using the byline below the headline title.

    Is it doable ?? Thanks a million!
  • Ashwin
    Jan,

    Yes it can be done with a trick by moving the byline before the headline and having just date in it.
    But having the date within the headline... I need to check...

    Regards,
    Ashwin
  • Bradley Gauthier
    Thanks for this, exactly what I was looking for... retweeted :)
  • Max
    Hey Ashwin,
    Can you please let me know as to how do I modify the date to be displayed on the main page in a square, just like the "retweet" button.
    Thanks!
  • Ashwin
    Max,

    Using CSS, you'll need to give a Background color and padding to the following element, to get the date inside a square:

    .custom .headline_meta .published { ... }

    Hope this helps.
  • manavecplan
    Hi Ashwin,

    Just wanted to say that you keep coming up with really helpful WP code that is EASY TO IMPLEMENT!This is the 2nd piece of code I'm lifting blind from your site... :-)

    Cheers,
    AviD
  • Ashwin
    Hey AviD,

    Great that you found it useful. Happy times with Thesis :)

    Regards,
    Ashwin
  • manavecplan
    A bit of help Ashwin....

    I'd like to integrate the following piece of code for my site with your code. Any ideas?

    // Custom Byline Function
    function custom_byline() {
    echo '

    Updated on ' . get_the_time(get_option('date_format'));
    if (!is_single())
    echo ' |  Posted In ' . get_the_category_list(',');
    echo '

    ';
    }
    add_action('thesis_hook_after_headline', 'custom_byline');
  • Ashwin
    This should be the merged code.

    function add_date_to_teaser() {
    //Number of days this post is old
    $daysold = (current_time(timestamp) - get_the_time('U') -
    (get_settings('gmt_offset')))/(24*60*60);

    global $thesis_design;
    $date_formats = thesis_get_date_formats();
    $use_format = ($thesis_design['teasers']['date']['format'] == 'custom') ? $thesis_design['teasers']['date']['custom'] : $date_formats[$thesis_design['teasers']['date']['format']];

    if ($daysold if (!is_single()) {
    echo '' . 'Updated On ' . get_the_time($use_format) . ' | Posted In ' . get_the_category_list(','). '' . "\n";
    echo ' | Posted In ' . get_the_category_list(',');
    } else {
    echo '' . 'Updated On ' . get_the_time($use_format) . '' . "\n";
    }
    }
    }

    add_action('thesis_hook_after_teaser_headline','add_date_to_teaser');

    Let me know if it works.
  • manavecplan
    Anything on this Ashwin?Still getting a parse error...
  • Ashwin
    It works for me. I am wondering why it doesn't work for you.... Here is the code...

    function add_date_to_byline() {
    //Number of days this post is old
    $daysold = (current_time(timestamp) - get_the_time('U') -
    (get_settings('gmt_offset')))/(24*60*60);

    if ($daysold if (!is_single()) {
    echo ' Updated on ' . get_the_time(get_option('date_format')) . ''.' | Posted In ' . get_the_category_list(',').'';
    } else {
    echo ' Updated on ' . get_the_time(get_option('date_format')) . '';
    }
    }
    }

    add_action('thesis_hook_byline_item', 'add_date_to_byline');

    You can find it working at:
    https://thoughtsunlimited.net/apps/sandbox/

    On Home page you see the categories - and on single post pages you don't.

    HTH. Let me know if it works.

    Thanks.
  • manavecplan
    Thanks so much for sandboxing it for me Ashwin! Unfortunately...still no go. Just a question though, did you remove the 30 days check function from your latest modified code?

    I think I'm just going to go with your original post code because
    A) I really like and NEED the functionality it provides.
    B) Formatting is nice but not critical.

    :-)
  • Ashwin
    I just made it 90 because on my sandbox site - the oldest post is in January.

    The code should work with '30' also.
  • Ashwin
    AviD,

    I didn't manage to look at this last weekend. I will check it out today and get back to you.

    Thanks,
    Ashwin
  • manavecplan
    Nope....getting a parse error on this one.
blog comments powered by Disqus

Previous post:

Next post: