How to Show Content in WordPress, Based on the Publish Date
Many of you may notice that if you go back and view some of the older content on the site, you see adsense, or in some cases, adbrite (an adsense alternative) ads. You won’t however see it, if the content is less than 10 days old.
So… why am I showing ads based on the publish date and most importantly, how did I do it?
Why Show Date based Ads
It should not come as a surprise that I have a lot of indepth content on this site. Most articles are more than 1000 words and simply due to poor structure on my part, is hard to find for on site visitors. That means a lot of the traffic to those older articles comes from search engines or referrals from links on other sites. I can live with that…
Since that content is buried, I wanted to find a better way to monetize it and decided to start showing contextual ads on older pages. (aka adsense and adbrite)
In my case, (and really the case on blogs in general) old is +10 days. This way, visitors who come through the rss or email subscribers feed are not bombarded with ads the minute get to the site.
How to Show Date Based Content
For ads, like I have mainly used it for, you CAN find adsense plugins to do the heavy work for you. I am choosing not to use a plugin for 2 main reasons:
- Each time you install a wordpress plugin, you increase the server load and it might slow down your site.
- I want to use it for more than just adsense in the longrun.
- Making Small Code Hops like this Sharpen Up ALL Other Code Skills
The meat and potatoes… (Here is the code in a text file)
- We need to know the value for 10 days ago. (You can see I put a -10 in red, if you wanted 3 days, it would be -3)
- We need to know the value for the post date.
- We need to supply the “if <then> then” (and maybe even a <else>) rule, to display what we want.
<?php
// Get the value for 10 days ago
$tendaysold = date(“Y-m-d”,mktime(0,0,0,date(“m”),date(“d”)-10,date(“Y”)));// Get the value of the article date
$postdate = the_date(‘Y-m-d’,”,”,false);?>
Then, inside the theme file where we want to show specific things based on the date or age of an article, we add our if<>then.
<?php if ($postdate < $tendaysold) { ?>
<!– this is where you put ANY code you want to display if the post is MORE THAN 10 days old –>
<?php } else { ?>
<!– this is where you would put stuff if it was LESS THAN 10 days old–>
<?php } ?>
In my case, for adsense, the whole thing runs like:
<?php
$tendaysold = date(“Y-m-d”,mktime(0,0,0,date(“m”),date(“d”)-10,date(“Y”)));
$postdate = the_date(‘Y-m-d’,”,”,false);
if ($postdate < $tendaysold) {
?>
<div style=”display:block; text-align:left; margin:20px auto 35px auto;”>
<script type=”text/javascript”><!–
google_ad_client = “pub-yourownadsensepubid”;
google_ad_width = 336;
google_ad_height = 280;
google_ad_format = “336x280_as”;
google_ad_type = “text”;
google_ad_channel = “your_ad_channel”;
google_color_border = “FFFFFF”;
google_color_bg = “FFFFFF”;
google_color_link = “0000FF”;
google_color_text = “cccccc”;
google_color_url = “000000″;
//–></script>
<script type=”text/javascript”
src=”http://pagead2.googlesyndication.com/pagead/show_ads.js”>
</script>
</div>
<?php } else { ?><?php } ?>
Other Things You Might Use This For
- Showing or Hiding Comment Dates
- Showing product auction listings, or amazon products, or vendor specific ads.
- Post Dates themselves – Maybe you want to hide the date after 30 days, sine it may turn visitors away!
- Adding an Image like “NEW” or “Just Added”, next to your post title.
The uses are endless…
What Else Might it Be Useful For?
Share your ideas below….
Previously Published Articles You May Like to Read:
- Build an Authority Website Based on a Knowledgable Topic!
- Day 1 – Publish The first 10 Posts and Invite the Search Spiders!
- Does Aggregated Content Violate Copyrights?







Mark,
As per your quote….
“Each time you install a wordpress plugin,you increase the server load and it might slow down your site.”
Would this also apply even if your plugins are de-activated..I always
wondered about this. Should you delete plugins you are not using..
@David – I HAVE definitely seen deactivated plugins still have an effect David!
I usually delete if not needed.
The BIG downside to plugins is that many of them load on EVERY PAGE, regardless of if they are needed or not. (Like contact form javascript, css, etc)
This post for instance has extra codee loading for posttabs – which are not being used. Just a waste of code and load time!
Mark
Mark,
This is brilliant! Thank you so much for the code.
Rochelle
hmmmm – interesting – just had to chime in and say thanks for that code – now i know how to code my sites for adsense within the content with a DIV. I generally remove all the dates on my post/pages.
NP Rochelle – hope it comes in handy!
@ Sean – I hear ya on the dates…
That div is styled with what is called “Inline Style”… and works on just about ANY html tag.
< p style= >
< h1 style= >
< font style= >
etc…
Mark
Mark – this is great! I’ve been wondering how to get rid of the dates on posts, and this should do it. Can you tell me where I would insert this code? I’m still a total coding noob, and not sure where I should put it.
Thanks as always for your help!
@Alice – Hey Alice, Great to hear from you and see you here!
Where the code goes is VERY dependent on the theme and varies from theme to theme.
On the Arthmia, it goes into the single.php, but on flexibility2, it goes in the index.php file.
Mark
Hey Mark, thanks for your quick response!
I had a feeling that I was gonna get an “it depends…” answer! ;-)
So, just in case I can’t figure it out, what are the names of some plugins that remove the dates? I may just have to go this route.
Have a great day!
@Alice – I really don’t know about plugins for that Alice, nor would I think you want the overhead of a plugin to remove one small line of code. (Plugins slow down site speed)
If your theme uses a single.php file, you can use firebug for firefox to id where the changes need to be made pretty easily.
Search your code for: the_time and it will show where the date functions are.
Mark
Leave your response!
Listen While You Read!
Site Categories
Monthly Archive
My Favorite Places
Blogging Sites
Blogroll
Places I Write
Technology Sites
Site Credits
Niche Store Builder is powered by WordPress, using a modified theme originally inspired by Arthemia.
Home | About | Terms & Privacy