Monthly Image in PHP

This is a small change that I applied to the WordPress theme on this site. It changes the header image every month. If you like to use this, put the following tag into your header or modify any image in any template- (php-file) on your site.

<img id="frontphoto" src="<?php bloginfo('template_directory'); $monthnum = date("m"); echo "/img/homepix_".$monthnum.".jpg"; ?>" width="760" height="140" alt="" />;

In this code snippet, the following three things are important:

  • bloginfo('template_directory'); echoes the template directory. You could alternatively put your images somewhere else and just use an absolute path.
  • $monthnum = date("m"); assigns a two-digit month number to the $monthnum variable.
  • echo "/img/homepix_".$monthnum.".jpg"; echoes the folder and the rest of the file’s path.

This assumes that the images are named “homepix_03.jpg” (e.g. for the March image) and are placed in the /img directory of your template folder. Modify this to suit your needs but keep the principle the same.

Comments and Reactions