Plays Well With Others

Date Specific Page Headers…

Let me roll up my sleeves and dig into some code for a second. Over the last few days I’ve been playing around with the image at the top of my pages—the one with the glasses, the name of the site, and the random sayings.

The random sayings are all separate image files, fueled by a little program called Randex. It’s an easy install and a program I’ve been using for YEARS, through the various incarnations of this website. It’s a simple PHP script that feeds ANY html code into a specific spot, so you have random images, random text, etc.

But back to my image header issue. What I was looking for is a way to make them date-specific. The first thought was to make twelve different image headers and name them “header_xx.jpg” where “xx” would be the php month as a 2-digit number. I was able to use WordPress’s date function header_< ?php the_date('m'); ?>.jpg to call the current month and then based on that information, the proper header would show up. Easy shmeezy.

But you know, being geeky and all, I wanted to go a step further. I wanted more control and so I started searching for some code that would allow me to be more date specific. For example, have a certain header only show up on December 25th, or have it only show up between December 23rd and the 25th. If a date wasn’t specified, it would revert to a generic header image. I found code that was kinda-sorta like what I wanted but not really. So I wrote something. Now, let me first say that I’m not a php coder by any means. If anything is wrong, I won’t be much help figuring it out. I just know it works on my site. Also, if anyone knows how to make it easier or more useful, feel free.

All of this code goes in place of the image you want to replace, so in place of my normal image header image tag, I’ll put all of this code and it works. For me, I put it in my header.php file. You could probably also put it all in an external file and call it via a php include. Also, you might have to be careful that you don’t use tags “$xxx” that already exist within WordPress, or it might mess something up. I ended up changing the code I’m using to have a prefix in front of each tag like “pwwo_ny” or “pwwo_val” just to be sure I wasn’t going to screw something up.

So that’s it. Basically I’ll have to keep on top of the dates for those holidays that fall on different days each year, but other than that, it should be fairly automatic through the year. Also, what I found is that I can use it for other things also, like a specific message that I want to automatically go up on my page. In place of the image tag, I could put other HTML that could be invoked.

This is VERY simple PHP coding and I’m not a coder, so I’ll pretty much delete any comments from hardcore coders that feel that need to point that out. Comments with constructive advice is always welcome. And if there’s a plugin that already does this, just kill me because I searched all over for one.

Note: After creating this entry, I noticed that the code was getting mangled on the page output, so I put the code into a text file you can get here.

View the headers here

Tags: , , ,

Comments are closed.