Displaying Date Specific Message on WordPress

There are ways to get creative with WordPress and you can pretty much do whatever you want to do with it. So, recently I was working on a project where my client wanted to display new message to his visitors every day. He wanted to set greeting message for special Holidays, like Independence day, Labor day stuff like that. So, I came up with a solution which is pretty simple an he liked it.

Since it is based on number of months and days, you can not add more than 365 statement (assuming 365 days a year). I 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';
   } elseif ((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), using PHP date function from the web server and displays the message accordingly. 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 if (function_exists('historic_day')) { 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 me from the comment section below.

Today In History

  •  
  •  

Comment

Leave a Reply

Note: Convet HTML, PHP, JavaScripts from HTMLify, before posting from comment section.
License: By submitting a comment here you grant this site a perpetual license to reproduce your words and name/Web site in attribution. Please use your real name or a pseudonym (i.e., pen name, alias, nom de plume) when commenting. If you add your site name, company name, or something completely random, I'll likely change it to whatever I want.