WordPress Theme Style Declarations

As some will know, I’m coding up a WordPress theme from scratch. The idea is to give lots of flexibility so that the theme can be easily modified using stylesheets alone. 

Out of the box, it’s fairly gaudy, but this is intentional as it encourages the modification! (Okay, I will ultimately make an ‘okay’ version based on the raw templates).

Anyhow, I thought to myself that it’d be great if css classes could be provided to allow pages to be styled differently depending upon category, tag or date. Lovely to be able to have a ‘Christmas theme’ that pops into being and goes away automatically without special coding and ‘if’ statements.

Therefore, I replaced the <body> tag in header.php with this code:

$today_month = date("m");
$today_year = date("Y");
echo "<body class=\"today_$today_year$today_month$today_day todaynoyear_$today_month$today_day year_$today_year month_$today_month day_$today_day todayyearmonth_$today_year$today_month ";
if (is_tag()) {
$current_tag = str_replace(" ", "_", single_tag_title("", false));
echo "maintag_$current_tag"; }
elseif (is_category()) {
$current_category = str_replace(" ", "_", single_cat_title("", false));
echo " maincat_$current_category";}
echo "">"; ?>
<!-- If a tag page, the tag is listed as a css class declaration in the form maintag_tagname -->
<!-- If a category page, the category is listed as a css class declaration in the form maincat_categoryname -->
<!-- There are also tags which update daily, letting the site owner have styles which only operate on certain days -->

On a tag page, it would do something like this: <body class="today_20081222 todaynoyear_1222 year_2008 month_12 day_22 todayyearmonth_200812 maintag_coding">

Thus, one could have specific CSS for a particular day (e.g. Christmas), or for a particular tag or category. All specific customisation can then be in the CSS.

About Murk

I'm interested in a range of things, from science and computers, to history and creative stuff. I'm leaving a lot out. My problem is time. As a result, murky.org, flits around from topic to topic as my interest is piqued. It's all tagged though - so it should be easy to find other posts of mine on similar topics - do give the RSS feed a try, and I welcome, nay, I crave comments on articles.

One thought on “WordPress Theme Style Declarations

  1. I also wanted to add a tag on a date based archive, or a single page archive, to refer to the date of that archive/post (rather than current date), but I couldn’t work out how to get the date of the current post when outside of ‘the loop’.

Leave a Reply