Tech Notes

Dress Up Your Blog for Darwin Day

Or any day for that mat­ter. How to post spe­cial logos for spe­cial days with a lit­tle (very lit­tle) PHP. more →

Dress Up Your Blog for Darwin Day

My blog will be sporting a special logo for Darwin Day on February 12 only.

It is no exag­ger­a­tion to say that Charles Dar­win rev­o­lu­tion­ized our under­stand­ing of nature and our place in it; I’ve cited a few arti­cles that rec­og­nize his con­tri­bu­tions in Link­ing Think­ing. So, I’d like my per­sonal blog to join the Feb­ru­ary 12 cel­e­bra­tions that will mark the 200th Anniver­sary of Charles Darwin’s birth and the 150th anniver­sary of his book, On the Ori­gins of Species. In the geeky tra­di­tion of Google and Yahoo!, I’d like to dress my blog for Dar­win Day with a cus­tom logo.

darwin-apeMy inspi­ra­tion for the cus­tom logo comes from a good-natured car­i­ca­ture of Dar­win as an ape pub­lished in 1871, a time of grow­ing accep­tance of the the­ory of evo­lu­tion. If an ape, why not a chi­huahua? After­all, Dar­win believed there was a con­ti­nu­ity among humans and all other species.

With logo in hand, I want it posted auto­mat­i­cally. Since I’ll be kick­ing up my heals on Dar­win Day, I won’t have time for any man­ual logo fid­dling. Because I am learn­ing PHP and have been mer­ci­lessly hack­ing the Word­Press tem­plates that run this site, my first thought was to use con­di­tional tags, specif­i­cally the is_day func­tion. Con­di­tional tags let you dis­play con­tent based on the con­di­tions you set. In this case, I want to dis­play a spe­cial Dar­win logo if the day is Feb­ru­ary 12. On all other days, I want to dis­play the usual chi­huahua logo. I was right about using con­di­tional state­ments, but wrong about the func­tion. The is_day func­tion lets you con­trol when a daily archive is being dis­played; I want to con­trol what con­tent is dis­played on a given day. Back to Google search for new tactics.

As is often the case in the World of Word­Press, some­one had already had the same bril­liant idea, fol­lowed the same mis­guided path and some­one else had already offered a bet­ter solu­tion. On the WP-Hackers mail­ing list, Samuel Wood aka Otto of ottodestruct.com points out that the cor­rect func­tion to use is getdate, and he pre­scribes a con­cise bit of code that gets the job done. Here is how I adapted it for my needs:

<?php
// Add special logos for special days
 
$today = getdate();
// Darwin Day is Feb 12
  if($today['mon'] == 2 && $today['mday'] == 12) { ?>
    <img src="<?php bloginfo('url'); ?>/images/darwin-logo.png" alt="logo for Darwin Day" width="135" height="150" />
    <p class="greetings">Happy Darwin Day. Celebrate science and reason on Charles Darwin's 200th birthday.</p>
  <?php }
 
// Christmas Day is Dec 25
  elseif($today['mon'] == 12 && $today['mday'] == 25) { ?>
    <img src="<?php bloginfo('url'); ?>/images/xmas-logo.png" alt="logo for Christmas Day" width="135" height="150" />
    <p class="greetings">Merry Christmas. Peace on earth and good will to all.</p>
  <?php }
 
// default logo for all other days
  else { ?>
    <img src="<?php bloginfo('url'); ?>/images/chihuahua-logo.png" alt="Portable Learner chihuahua" width="135" height="150" />
    <p class="greetings">Getting there is half the fun.</p>
  <?php }
?>

This code dis­plays spe­cial Dar­win and Christ­mas logos on the home page on Feb­ru­ary 12 and Decem­ber 25, respec­tively, and the reg­u­lar chi­huahua logo on all other days. Each time I want to rec­og­nize another sig­nif­i­cant day, I only need to add another elseif state­ment. Very nice, Otto.

Where does this bit of code go? Because I use the The­matic Word­Press theme, I slipped this code between a pair of <li> tags in the widget-ready area called index-top asides. I also use a plugin that allows me to use PHP code in widgets, such as Sören Weber's Exec-PHP plugin.

If you aren’t using such a flex­i­ble theme, or do not want to use Sören’s plu­gin, then save this code as a PHP file, say logos.php. Then, call this file into the header.php:

<?php include(TEMPLATEPATH . '/logos.php'); ?>

While my exam­ple focus on Word­Press, this code can be used in any other PHP-driven con­tent man­age­ment sys­tems such as Dru­pal and Move­able­type. Now, you can always have your blog cel­e­brate with you.

♦ ♦ ♦

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Subscribe without commenting