WordPress snippet #1

Useful little piece of WordPress code, plays a particularly useful part of a dynamic featured post slideshow that I was going to detail at some point. To reference the  set upload directory url in your WordPress theme:

<?php $upload_dir = wp_upload_dir(); ?>

<?php echo $upload_dir[‘baseurl’]; ?>

more info here http://codex.wordpress.org/Function_Reference/wp_upload_dir

I may be some time…

Next Wednesday I set off in a van on a 5,000 mile round trip to Istanbul to support the Istanbul to Leeds cycle challenge which aims to raise funds for the Jane Tomlinson Appeal. As a result I will be more-or-less out of action from 21st July through until 31st August – I will have some access to emails but will only be able to check and respond on an irregular basis.

Hopefully before then I will finish off sites for Will Soden (plumber), Pickles and Potter (Deli/Cafe) and the Northern Dales Farmers Markets (farmers markets) as well as getting on as much as possible with the new site for Run For All (mass-participation runs). Diverse to say the least!

Oh and as a slight aside I also found something else that results in WordPress’ wonderful white screen of death – messing around too much with the chmod settings of the wp-content folder.

Sharing is caring

WordPress – styling by category

I’ve spent the best part of this morning working out how to add a post’s category to its h2 tag’s class – which then allows me to do category-specific styling. Essentially I wanted each headline to be a different colour based on its category – simple result, slightly convoluted solution.

Anyways, it took a while so I thought I’d share it here to save others the hassle!

You first need to add the following to your functions.php file

<?php
function the_category_unlinked($separator = ‘ ‘) {
$categories = (array) get_the_category();

$thelist = ”;
foreach($categories as $category) {    // concate
$thelist .= $separator . $category->category_nicename;
}

echo $thelist;
}
?>

Basically this takes the results of the usual the_category() query – i.e. an unordered list – and strips out all the list formatting and presents the results as a nice list of the categories, with each category separated by a space.

You can then add this to your h2 (or whatever tag you want to style) class=”ADD HERE” in your index.php file (or wherever you need)

<?php the_category_unlinked(‘ ‘); ?>

This pulls the category info as formatted by the function you just wrote in the functions.php file – i.e. without any list formatting.

You then just need to add the relevant class styling info (e.g. .categoryname{some styling}) to your styles.css file and you’re good to go.

Hopefully this was useful.

WordPress theme development and the white screen of death

I’m currently working on a new site for a client who requires a CMS. After a fair amount of research and based on my own experiences they decided that WordPress fitted the bill. So I needed to develop a bespoke theme for them.

Now I’ve messed around with tweaking wordpress themes before and it is pretty intuative. Unfortunately last night the whole thing came crashing down and I had to battle with the (apparently infamous) ‘white screen of death’ – which usually seems to be caused by a php bug. These bugs can be as simple and innocuous as an additional line break in your code (WordPress doesn’t like space it would seem). I’ve not yet tracked down the source of my ‘white screen’ issues ( EDIT – I have since found the source of my bugs, it was caused by such an innocuous thing i still can’t quite get my head around it! basically i needed to remove and all space between each function in my functions.php file – i.e. so there was no space between each closing ?> tag and the following opening <?php for the next function. it’s ridiculous that such a small thing brought everything crashing down, but there you have it…) but if you’re suffering from something similar then there are numerous lists that should help troubleshoot the problem. Here are just a few that I’ve found helpful:

http://wordpress.org/support/topic/405711

http://wordpress.org/support/topic/363816

http://www.amandavandervort.com/blog/2009/12/how-i-solved-my-wordpress-white-screen-of-death/

I’d also add that if you’re considering WordPress theme development then it’s a good idea to have a good read of the documentation first (there are lots of dependancies that it’s useful to be aware of so the whole thing doesn’t come crashing down) http://codex.wordpress.org/

And there are also a number of very good tutorials around WordPress theme development. Again, here are a few I’ve found helpful:

http://www.wpdesigner.com/2007/02/19/so-you-want-to-create-wordpress-themes-huh/

http://themeshaper.com/wordpress-themes-templates-tutorial/

http://jonathanwold.com/tutorials/wordpress_theme/

http://codex.wordpress.org/Theme_Development

http://codex.wordpress.org/Blog_Design_and_Layout

http://net.tutsplus.com/articles/web-roundups/top-50-wordpress-tutorials/

http://net.tutsplus.com/site-builds/how-to-create-a-wordpress-theme-from-scratch/

Hope that helps!