• Translate this post automatically to any language you want!

WordPress Tricks – Part 3

HOT!

9 April 2011

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.

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

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.

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:

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

Enjoy!


Jany Martelli

Founder & CEO

I started programming at 7, assembled my first PC at 10, made the first website at 13, studied Business Management and Marketing at the University of Florence and dropped out at 23 to run my own thing, Shambix.
I love raging to EDM, snowboarding,  travelling, driving off in the Tuscan countryside and… writing code (read “being a nerd”).
Eventually I’ll get my degree as well.

Develop (HTML 4/5, CSS 2/3 + .less, PHP, Javascript, Twitter Bootstrap Framework, WordPress official consultant), design (Photoshop, InDesign, Illustrator), plan (Business Development, New Products, Marketing Strategies), manage.


6 comments on “WordPress Tricks – Part 3

  • Thank you for the wp_list_categories bit!

    Reply

  • Ma se invece volessi escludere le miniature in tutti i miei post come devo fare? Di default, quando inserisco l’immagine in evidenza, compare una thumb che non voglio assolutamente… ho individuato (credo) la parte di codice che mi iteressa:

    add_theme_support( ‘post-thumbnails’, array( ‘post’ ) );
    set_post_thumbnail_size( 130, 130, true ); // Normal post thumbnails
    add_image_size( ‘single-post-thumbnail’, 250, 250 ); // Permalink thumbnail size

    ma non capisco un granchè! aiutatemi!

    Qui si vede in piccolo l’immagine in evidenza che ho inserito (e mi sta bene)
    http://www.fotobaby.it/fotografi%20catania/galleria-foto/

    Qui invece la stessa immagine risulta li in alto a dx ma non la voglio! Come toglierla solo dai post?
    http://www.fotobaby.it/2011/09/lorenzino-e-mamma-valentina/

    Reply

  • Ma il “crop” dove l’hai messo esattamente?
    Il “true” della stringa che ti dicevo, istruiva appunto la funzione, di fare il crop all’immagine, che è tagliata perchè il crop fa appunto quello.
    L’immagine non la vedi per una questione di CSS.
    Metti nel css:
    .related-posts img {position:absolute;}
    ol.related-posts p {position:relative; top:85px;}

    Reply

  • Gunela

    Ciao, caspita che velocità, sono commosso! :-)
    Il tuo escamotage purtroppo non ha funzionato. Dentro functions.php ho messo la stringa che mi hai suggerito ma non ha prodotto effetti.
    Ho risolto parzialmente il problema inserendo un crop nella parentesi dell’array.
    Mi taglia l’immagine ma per lo meno il thumbnail è quadrato.
    Ora ho un altro problema: con IE7 non visualizzo l’immagine e il relativo link.. avete qualche idea del perchè?

    Reply

  • Ciao Davide, metti questo nel functions.php:
    set_post_thumbnail_size( 80, 80, true );
    Dimmi se è quello che intendevi!

    Reply

  • Davide

    Ciao, ho implementato la tua funzione sul mio blog adattandola per un template di yarpp. E tutto perfetto se non i thumbnail estratti dalle Immagini in Evidenza dei post, che riflettono la loro dimensione.
    Es: sul post l’immagine è rettangolare.
    Dentro la mia funzione ho settato 80×80, mi aspettavo una immagine quadrata e invece è 80×53. Come posso risolvere la questione, se possibile evitando di distorcere l’immagine?

    Reply

Leave a Reply