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.

$catlist = explode( '<br />', wp_list_categories( '&title_li=&style=none&echo=0' ) ); // add whatever args you want, just don't remove the current ones
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:

$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.

// Make sure the template supports featured images
if (function_exists('add_theme_support')) {
// 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.'" />'; 
 }  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 ';

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

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


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.

Privacy Preference Center


Also called a transient cookie, a cookie that is erased when the user closes the Web browser. The session cookie is stored in temporary memory and is not retained after the browser is closed. Session cookies do not collect information from the user s computer.

They are usually used to temporarily keep track of users' preferences (eg. currency, language, items you place in the cart - in case of an e-commerce website, which this website is not) throughout the website. Other times they are used to make sure the user is not a malicious bot trying to take down a website, hence the cookie cannot be disabled for security reasons.

They are usually harmless (or they should be!) and will go away when you leave the website or close the browser.



Persistent Cookies.

Also called a permanent cookie, or a stored cookie, a cookie that is stored on a user's hard drive, until it expires (persistent cookies are set with expiration dates) or until the user deletes the cookie.

These cookies are meant to profile the website's visitors and let the admins know who visits their website. Your name is NOT tracked, but your IP, country, browser and device specs, research terms and more, usually is. It helps admins understand what contents their users like best and who is their audience, in order to provide better and more targeted content.

Other cookies have the same functions as session ones: for example, they simply help the browser remember if your Internet connection is fast enough to play high-quality videos next time, or what language you chose for your subtitles.

Some of these might be relatively harmelss, since they were meant to first and foremost help admins to manage their site's audience and content, however they might also be used, to be collected and sold to third parties.

You can check a full explanation of a few of the most common ones here



Persistent Cookies.

Also called a permanent cookie, or a stored cookie, a cookie that is stored on a user's hard drive, until it expires (persistent cookies are set with expiration dates) or until the user deletes the cookie.

These cookies are meant to collect your browsing preferences (your searches, the websites you visit, things you like or don't like, personal conditions and things you might not want a third party to know about nor to be sold to someone else).

A vast majority of websites, search engines, apps and social networks, will place these cookies in your browsers because they are showing you ads and making money off them. This is not inherently bad, however the ad you see, comes also with these cookies, to help Advertising Networks to show you even more relevant ads, as well as collect a vast amount of data about Internet users, to then be sold or mishandled in ways that could harm society.

This website shows Ads from Google, therefore it places their cookies in your browser (sorry!).
If you don't wish to be tracked, please adjust your privacy settings here.
We also show Facebook Ads, therefore if you don't wish to be tracked by them, please head over here to change your settings.

Also turn these cookies' consent toggle OFF.

Please understand that not allowing Advertising cookies to be placed in your browser, does NOT mean you will not see ads, you STILL will, but they simply will not be relevant to your own interests anymore.

On Chrome, click here, to block all third party cookies (they might all be used for advertising or collect your preference to be sold to the highest bidder, though, but most likely they will be).

If you want to have even more control over which third parties can collect your data (even outside this website), please visit this website.