How to Display Date Specific Message on WordPress

by • On Feb 24, 13 14:39pm • 3803 Views

WordPress Tips and TricksIf you are visiting our site lately, you may have noticed that underneath our category menu we display date specific event that took place in history. We try to focus more on events that has something to do with “technology”. Every single event is linked with a Wikipedia article so that user can simply click on the link to get little more detail info on that specific event. Part of the reason why we did that is because we wanted our visitors to know more. It is more like giving you an extra dose of info that you may have not known before.

As the day goes by, the link and the content changes automatically. However, we need to add those text and links to an article manually. Since it is based on number of months and days, you can not add more than 365 statement (assuming 365 days a year). We implemented this partly automated system using a very simple function PHP function. Let’s take a look at the base code.

<?php
  function historic_day() {
   if ((date('m') == 1) && (date('d') == 1)) { echo 'Happy New Year'; }
   else if ((date('m') == 3) && (date('d') == 15)) { echo 'March 15'; }
   else { echo 'Random day'; } }
?>

What is happening here is that we created a PHP function called “historic_day()” and by using simple if else conditional statement, we are echoing any specific statement. This function checks the month (m) and (&&) date (d) from the web server and displays the event. For every month and day, you can assign one or more events. In case, if you don’t have events for any specific month and date, it would simply display “Random Day”. There are so many ways you can customize it but this is the most simple form that we thought would fit our needs.

Now, you can simply copy and paste the function on your WordPress theme’s functions.php file and call the function anywhere within the template files of your theme. To call the function, use the following line and display it anywhere you want.

<?php historic_day(); ?>

I hope you liked our simple snippet code. If you have any question on this regard or encountered any problem while implementing this on your site, feel free to inform us from the comment section below. We will get back to you as early as possible.

You can follow us on Twitter, add on your Google+ circle or Like our Facebook page to get the latest news, updates and reviews.

advertisement

Comments