Aug
4
2008

Display And Hide Content Based On Date Or Age Of Posts – WordPress Hack

Share
Email

I posted a comment on Dates on Blog Posts – Should You Have Them? last week over at Problogger.Net on how easy it would be to implement code to a theme which would allow a user to show or hide content based on the date or age of a post. I’ve since gotten a few emails about this in particular and was going to share this with some of these tricks I had mentioned when I did the redesign of this site last week.

Straight from Problogger.net, here was the initial inquiry:

Dates on Recent Posts But Not on Older Ones - I saw one blogger do this last year (I’m afraid I don’t remember who it was). They had hacked WordPress so that dates appeared on recent posts (within the last 3 months) but anything older than that did not have time stamps either on the post or comments. This meant that the blogger benefited from new posts looking new and took the potential distraction of old posts away from readers. I don’t know exactly how the blogger did it but presume they set up a rule that looked at the date of authorship and then determined whether the date would be displayed or not.

In short, to do this you’ll be modifying your theme (/wp-content/themes/YOURTHEME/ index.php and/or single.php) rather then the core wordpress code. Also I’m using wordpress 2.6 but this should work on anything wordpress 2.0 or later for sure. Anything lower it might work but you might need to redefine $postdate with whatever the_date() function was back then. (It may very well be the same). Other then that, its good old PHP that handles it.

  1. <?php
  2. //File: /wp-content/themes/YOURTHEME/ index.php and/or single.php
  3. //FIND:
  4. if (have_posts()) : while (have_posts()) : the_post();
  5.  
  6. //ADD UNDER:
  7. $twomonthsago = date("Y-m-d",mktime(0,0,0,date("m")-2,date("d"),date("Y")));
  8. $twentydaysago = date("Y-m-d",mktime(0,0,0,date("m"),date("d")-20,date("Y")));
  9. $currentdate = date("Y-m-d",mktime(0,0,0,date("m"),date("d"),date("Y")));
  10. $postdate = the_date('Y-m-d','','',false);
  11. ?>
  12.  
  13. //ADD WHERE APPROPRIATE
  14. <?php
  15. if ($postdate > $twentydaysago) {
  16. echo $postdate;
  17. ?>
  18. //Insert HTML code here for data you want to show if the post is newer (within 20 days old of posting)
  19. <?php } ?>
  20.  
  21. //notice the greater than/less than sign flipped
  22. <?php
  23. if ($postdate < $twentydaysago) {
  24. echo $postdate;
  25. ?>
  26. //Insert HTML code here for data you want to show older than 20 days ago.
  27. <?php } else { ?>
  28. //Insert HTML code here for data that is newer than 20 days.
  29. <?php } ?>

This code must be placed within the loop. By that I mean, it needs to be within that if (have_posts())… bit as shown in the code. From there we’re grabbing the date using the_date(). You don’t have to use a separate variable for this, but I find it easier because I reference that variable more than once in the code.

Some Uses For Showing & Hiding Content
Among the uses that users have for this kind of implementation:

  • AdSense or other Ads – Showing or hiding Ads on pages only after a certain date. Readers that keep up with your blog and look at all your new posts right away will appreciate this. They are also your most loyal readers
  • Social media tags – I’m doing this right now on this site. If the post is older than 20 days, I have it set to display social bookmarking links at the bottom of the post. If its newer, the bar displays at the top.
  • Contests – If you have different content you want to display before or after a contest, you can use this method to automate the content switch for you. For example. Before the contest you could wrap in your form code. After the contest date(s) end you could wrap in a “sorry, this contest is over” text.

Resources
Template Tags/the date – WordPress Codex – This is for modifying and understanding the_date() parameters in wordpress’s function.
http://us3.php.net/manual/en/function.mktime.php – This is the function we’re using within the date() function to “generate” a time. Note this time is based on your server time. Which means if your server time is an hour behind you, you can adjust for that by doing something like this (still assuming the two month string). date(“Y-m-d”,mktime(date(“h”)+1,0,0,date(“m”)-2,date(“d”),date(“Y”)));.
http://us3.php.net/manual/en/function.date.php – Date function calls/formatting in PHP. If you want to change the format of the date, this would be a good place to go. Just replace the configuration of the Y-m-d I have in there now to whatever format you want based on the chart shown there.

Hope that helps! Questions about this, post below. New requests or questions about the way I created this theme please post to the Rob Malon redesigned post.

Respond: Leave A Comment | Trackback URL

Entrupeners, Subscribe for the lastest tools, tips, and tutorials.


14 Responses to Display And Hide Content Based On Date Or Age Of Posts – WordPress Hack

  1. Rob, great post. Thanks for the detailed info. You can never have too many WordPress theme hacks in your arsenal.

  2. Do you have any idea how to adjust the CSS for a post on the index page by age?

    I would like to display the 5 most recent posts on the index.php but would like to have each ‘fade’ a little as they move further down the list. I can set up the classes in CSS to do this but I don’t know how to call them per post.

    Thanks in advance.

  3. Eugene

    Hello,

    Could you please post the code here that shows how to hide a post from the home page only? In other words, the post should show up under a certain category, but not on the home page.

    Thanks.

  4. What happens if you use a cache system like super cache?

    Im trying to make a system to only show ads for posts older than 3 days. Do you think this could be done with javascript?

  5. @gustavo – Good question. Super Cache has a syntax for dealing with functions or included files (with any code you want in it) in which you don’t want cached. The documentation on this is severely lacking but its possible. Since you sparked the idea I might write a howto about it when my current project is finished in a month. Check back then if you’re still stuck.

  6. Hey, thanks for this post.nnI want to do something similar. I would like to be able to make visible posts created only *after* the date the user created an account on my WP blog.nnSomething likenn###nif (date_user_registered is newer than post_date)nnthen have the post visiblennelse completely hide the postn###nn How would you go doing this?nnThanks!

    • Anonymous

      When you’re displaying the posts with the query_posts function (I assume) you can filter by date. Scroll a little more than halfway down the page there or search for “date”.nnhttp://codex.wordpress.org/Function_Reference/query_postsnnInstead of making one up like they are you can write a query which grabs the value for user_registered out of the wp_user table.nnI’m not sure I understand the scenario where that would ever happen though since users have to first sign up and then post.

  7. Jami

    Hi Guys,

    Can you tell me how I can hack the code to only show posts published in the last 6 months?

    I’d much rather do this via code than plugin..

    Ta

    Jami

  8. Nick Hesson

    I want to have a category display posts that are only from today and in the future. anything older than today i dont want to show. How do i do this?

    • I don’t understand why you’d want to have future posts display. WordPress has a scheduling system for future posts. If you mean to just display the titles then a quick dirty method would be to create a custom query and append the results under the code above (after specifying a 24 hour span only).

      I dont think the posts loop supports future posts either, but I could be wrong. Check the documentation. If you can change the query that is sent to has_posts you’ll be able to do it without a 2nd custom query.

      http://codex.wordpress.org/Function_Reference/have_posts

  9. bagus

    Hi, I’ve got a case nearly alike yours (but little different). My boss asking that all post that starting from current date until a month before being marked with the word “New”. Can I do with tag based on the date, so I don’t have to manually marking post with “New”?

Leave a Reply

Your email address will not be published.

*

Custom Theme by Rob Malon | Content & Design © 2010 - RobMalon.Com - Chicago, Illinois