WordPress Tricks #3


So here’s some more useful code for when WordPress, in its almighty awesomeness, forgets bits and pieces..
Let’s get started with a trick and a half: how to display WordPress categories called by wp_list_categories, one next to the other, also adding a separator in between them, but without showing it even after the last category.

<?php
$catlist = explode( '<br />', wp_list_categories( '&title_li=&style=none&echo=0' ) ); // add whatever args you want, just don't remove the current ones
array_pop($catlist);
echo implode(' / ',$catlist); // I chose the separator " / ", but you can put whatevs
?>

In case you actually want the separator to show up even after the last category, use this other bit of code instead:

<?php
$catlist = wp_list_categories('echo=0&style=none'); // add whatever args you want, just don't remove the current ones
$catlist = str_replace('<br />','|',$catlist); // so here I chose the separator " | ", up to you what to use
echo $catlist;
?>

And now a cookie for you, a function I made some time ago for Vivanotte.it, to simplify and automate post images displaying in any part of the template I wanted.
You will need timthumb for this, you will put it in a scripts folder, inside your template (put timthumbs.php and create an empty folder cache, making sure it’s writeable).
Then go ahead and put the code below in your template functions.php (not there? create it).

Moreover, thanks to timthumb, we can resize images nicely with any size we want and also have the images cached, so that the server doesn’t have to cry in pain, resizing every image each time run-time.

So basically, the function looks for, in priority order: Featured post image, image link added through a custom field thumb inside the post, any image uploaded inside the post.
The first one that it finds, will be the one displayed wherever we want in the template.

<?php 
// Make sure the template supports featured images
if (function_exists('add_theme_support')) {
  add_theme_support('post-thumbnails');
  }
// Let's do this!
function shambix_thumb($width,$height) {
global $post;
$key1 = "thumb"; // this is the name of the post custom field
$thumb = get_post_meta($post->ID, $key1, true);
$thumbnail = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'full');
$attachments = get_children( array('post_parent' => $post->ID, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'orderby' => 'rand', 'numberposts' => 1) );
 if (function_exists('has_post_thumbnail') && has_post_thumbnail($post->ID)) {
// the featured image
echo the_post_thumbnail(array($width,$height)); 
 } elseif ($thumb == true) {
// the image link found as value for the post custom field "thumb"
 echo '<img src="'.get_bloginfo('template_url').'/scripts/timthumb.php?src='.$thumb.'&w='.$width.'&h='.$height.'&zc=1&q=100" alt="'.$post->post_title.'" />';
 } else if ($attachments == true) {
// a random post uploaded in the post
 foreach($attachments as $id => $attachment) {
 $img = wp_get_attachment_image_src($id, 'full');
 $img_url = parse_url($img[0], PHP_URL_PATH);
 print '<img src="'.get_bloginfo('template_url').'/scripts/timthumb.php?src='.$img_url.'&w='.$width.'&h='.$height.'&zc=1&q=100" alt="'.$post->post_title.'" />'; 
 break;
 }
 }  else { 
// if nothing is found, don't display anything, however you could add some standard image in this case, up to you
 }
}

I’ve already SEOd the function, so that every image is validated and SEO friendly, with its alt = post title.
Pretty obvious but nontheless worth to mention, in case you want to link every displayed image to its post, follow the instructions:

// place it in the function above, after each echo' or print' but before <img src=...
<a href="'.get_permalink($post).'" title="'.$post->post_title.'">
// and this after the /> but before ';
</a>

So, to display post images anywhere in your template, simply use inside a loop:

<?php
shambix_thumb(width,height); // where widht and height are obviously the size you want, but without 'px'
?>

Enjoy!

Jany Martelli

I’m a Digital Consultant, Front-End Developer, specialized in WordPress.
I help companies, professionals and startups grow with technology and a winning digital strategy, I hand-pick and coordinate the perfect team for them, plan projects from the ground-up, develop & deploy websites, online stores and apps.