WordPress 2.9 +: new feature post thumbnails

With the latest release of WordPress 9.2 + from, we have introduced a new feature very useful to manage thumbnails, or thumbnails. This feature, however, must be activated in the theme, namely, acting on the file functions.php . This new feature is useful in many contexts and provides a simple and convenient tool for both the developer of a website using WordPress is the final customer who will then manage the content.
In versions of WordPress prior to 2.9, in fact, many developers used the custom fields (custom fields) to provide customers the ability to add an image - external - to the post. Solution from the point of view that if the developer was not particularly burdensome, rendeve still editing the post quite inconvenient, especially if we consider that often it is the end customer to treat the content. Other solutions could be to use those plugins already made, or write an ad-hoc. With release 9.2 +, however, you can enable management "miniatures" with a few lines of code, finding himself in the backend everything already done, making the process of insertion of the post really more comfortable and clear.

Activate the new function, post-thumbnails

To enable the management of the backend "Thumbnail article" is simply edit the file functions.php and add the following line of code:

1
) ; add_theme_support ('post-thumbnails');

Or, better yet, in view of compatibilià and stability:

1
2
3
function_exists ( 'add_theme_support' ) ) { if ( function_exists ('add_theme_support')) {
) ; add_theme_support ('post-thumbnails');
}

Without this, coming and going to change in the administration of a post, we will find a new panel on the right sidebar:

Nuovo Pannello Post-Thumbnails

Clicking on "Set Thumbnail" will open the classic panel already used to attach images and media. When we chose our image, insert it when we have the new "Use it as a miniature":

Utilizzarla come miniatura

Activation can also be controlled using best:

1
2
3
4
/ / Enable only for Post
, array ( 'post' ) ) ; add_theme_support ('post-thumbnails', array ('post'));
/ / Enable only for Post
, array ( 'page' ) ) ; add_theme_support ('post-thumbnails', array ('page'));

You can also choose the size of this "miniature", driven, besides working in the new editor of images made available from WordPress:

1
2
/ / 50 pixels wide by 50 pixels tall, box resize mode
, 50 ) ; set_post_thumbnail_size (50, 50);

Use the thumbnail

At this point all that remains is to use the thumbnail in our template:

1
the_post_thumbnail ();

Following the standard WordPress is also available without echo :

1
get_the_post_thumbnail ();

It is also possible to pass a series of parameters to this function, making it very versatile, as:

1
2
3
4
5
6
/ / Display the thumbnail
) ; the_post_thumbnail ('thumbnail');
/ / The version Medium
) ; the_post_thumbnail ('medium');
/ / The Large Version
) ; the_post_thumbnail ('large');

, cosa comodissima all'interno di “loop” personalizzati: You can also set other parameters as attributes of the tag img alt and title , which is convenient in "loop" custom:

1

Also for "retrocompatibilià" a good implementation of this new feature might be:

1
2
3
4
5
6
7
8
9
10
11
12
function_exists ( 'has_post_thumbnail' ) ) && ( has_post_thumbnail ( ) ) ) { if (( function_exists ('has_post_thumbnail')) && (has_post_thumbnail ())) {
/ / Wordpress 9.2 + using the embedded thumbnail
the_post_thumbnail ();
{ Else {}
/ / Wordpress 2.8 and above - use the technique of the custom field
/ / Use a custom field called "field-thumbnail" or the way you want it
/ / Contains the URL of an image
get_post_meta ( $post -> ID , 'campo-thumbnail' , true ) ; Postimage get_post_meta = $ ($ post -> ID, 'field-thumbnail', true);
$postimage ) { if ($ postimage) {
. $postimage . '" alt="" />' ; echo '<img src="'. $postimage.'" alt="" />';
}
}

Additional notes and examples

This feature is very useful and has been introduced in a comprehensive manner. I suggest you still see the official put the WordPress documentation detailing the entirety of all the functions and their parameters. As a further example we show how to set up a thumbnail of 64 × 64 pixels with a hard-crop and use the same image in each post by changing the settings of the width (eg 512) and height (infinite inserting 9999):

1
2
3
4
/ / In file functions.php
) ; add_theme_support ('post-thumbnails');
, 64 , true ) ; set_post_thumbnail_size (64, 64, true);
, 512 , 9999 ) ; add_image_size ('single-post-thumbnail', 512, 9999);

On the home page, for example, we will use:

1
the_post_thumbnail ();

In the file single.php using:

1
) ; the_post_thumbnail ('single-post-thumbnail');

47 comments to "WordPress 2.9 +: new feature post thumbnails"

  1. April 9, 2010 Saidmade Labs' web site design, web application development, viral marketing:

    [...] WordPress 2.9 +: new feature thumbnails post I explained how to use the new management features thumbnails, thumbnails, [...]

  2. April 12, 2010 joseph:

    but what is the difference between these 2 lines? that generates results??

    On the home page, for example, we will use:

    1
    the_post_thumbnail ();

    In single.php file using:

    1
    the_post_thumbnail ('single-post-thumbnail');
  3. April 12, 2010 Giovambattista Fazioli :

    @ Joseph: the first shows a thumbnail at the default size, those set by:

    1
    , 64 , true ) ; set_post_thumbnail_size (64, 64, true);

    the second is used to set a thumbnail our personnel with different dimensions, for example:

    1
    2
    , 128 , 128 ) ; add_image_size ('my-thumbnail', 128, 128);
    thumbnail ' ); the_post_thumbnail (I - thumbnail ');

    In this way we can have different thumbnail depending on our needs, and pages of different contexts. All at once realized by setting the "miniature".

  4. April 28, 2010 How to set automatic thumbnails of all post | Undolog.com :

    [...] New features miniatures of WordPress, introduced with version 2.9 and discussed in WordPress 2.9 +: new feature post thumbnails, can be automated using a simple PHP script. This can be handy when [...]

  5. July 13, 2010 Emanuel:

    Greetings to all,
    how can I change the name (label) in the administration of the thumbnail. Instead of "image evidence" of course in Italian.
    Currently I have started using wordpress 3.0 with the plugin "multiple post thumbnails."

  6. July 14, 2010 Giovambattista Fazioli :

    @ Emmanuel: I do not understand what you mean, did you mean to "Set Thumbnail"? You can try using the filter:

    1
    2
    3
    4
    5
    6
    $content ) { custom_admin_post_thumbnail_html function ($ content) {
    / / $ Content is in the output shown in the backend
    / / "Set Thumbnail"
    }

    , 'custom_admin_post_thumbnail_html' ) ; add_filter ('admin_post_thumbnail_html', 'custom_admin_post_thumbnail_html');

    If I misunderstood the question riptoponi. Greetings :)

  7. July 14, 2010 Emanuel:

    @ Giovambattista Fazioli:
    I mean the label of the field where it says thumbnail "image evidence" ;)
    I would be interested in facts also change the internal link "Set image evidence", in practice, as you know is the link that allows you to upload the image library ;)

    Meanwhile, thanks a lot

  8. July 14, 2010 Emanuel:

    @ Emmanuel:
    Ooops sorry I am using wordpress 3.0 as the Italian translation of the label has just "image evidence" while the first was (as in your tutorial) "Article Thumbnail".

  9. July 14, 2010 Giovambattista Fazioli :

    @ Emmanuel: Ok, now clear to me. Simply enter this code in the file functions.php of the theme active you are using:

    1
    2
    3
    4
    5
    customposttype_image_box function () {
    , 'post' , 'side' ) ; remove_meta_box ('postimagediv', 'post', 'side');
    , 'Quello che vuoi' , 'post_thumbnail_meta_box' , 'post' , 'side' , 'low' ) ; add_meta_box ('postimagediv', 'What you want', 'post_thumbnail_meta_box', 'post', 'side', 'low');
    }
    , 'customposttype_image_box' ) ; add_action ('do_meta_boxes', 'customposttype_image_box');

    Replaces, possibly using the locale, the string Quello che vuoi want with That chee :) excuse the game of "texts". I hope you will help.

  10. July 23, 2010 Merlinox :

    Thanks for this post!

  11. July 28, 2010 DOZ :

    Thanks for the clarification on the use of the new function, which hitherto had remained obscure to me!
    A question: is there a way - or plugin - automatically create a grid of "thumbs", hinting Article of belonging?
    (Any help is appreciated)

  12. July 29, 2010 Giovambattista Fazioli :

    @ Doz: of course! All you can do it without a plugin. Just create a function in the functions.php like this:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    creaGriglia function () {
    ; global $ post;
    new WP_Query ( 'category_name=blog&showposts=10' ) ; $ ThumbnailsPosts WP_Query = new ('category_name = blog & showposts = 10');
    '' ; $ O ='';
    $thumbnailsPosts -> have_posts ( ) ) { while ($ thumbnailsPosts -> have_posts ()) {
    the_post ( ) ; $ ThumbnailsPosts -> the_post ();
    '<a href="' . get_permalink ( ) . '" $ O. = '<A href = "'. Get_permalink (). '"
    }
    ; echo $ o;
    }

    Without this, the theme file index.php for example, you can call this function, adding the point that you see fit:

    1
    ?> <? Php creaGriglia ()?>

    . In the function creaGriglia() above I extracted the first 10 posts from category blog . By changing the parameters of the query and the HTML, you can easily create one you requested. Above, you see, I put the "thumbnail" images next to each other. By inserting the "tagging" the appropriate output format you as you see fit. Hope that helps.

  13. July 29, 2010 DOZ :

    @ Giovambattista Fazioli:
    Heila, that speed, which disponbiiità! ;)
    Just a moment I try your code, thanks a lot!
    HELLO

  14. August 10, 2010 Lorenzo :

    I'm testing the code since the release of 2.9 and the hard-cropping does not work mica.Su this point of view timthumb works much better if it were not that the output image is of a much lower quality than the input.

    I set a thumbnail cropping 60x60px with hard, I loaded a 600x300px image cropping is not hard but the function not want to work ... any ideas?

  15. August 22, 2010 sandro :

    hello .. and little I use wordpress code and understand it less than zero .. My probema and .. I posted on the home page images of size 320 x 320 obtaining the code from imageshak .... and comparing it with the entire post that image out of tune by being too broad and so the order of 10 posts per page This is long ... now I have searched the web but have not found the solution I was looking for ... or will ask that images can be reduce size of example 100 x 100? bearing in mind that once used the read more page that highlights the natural image 320 x 320 .... now having had experience with this blogger template in your code was the editing of the images on the home page .. wordpress may have the same function or should opt for other solution? .. thanks to those who will know how to give useful advice on how to proceed ... sandro

  16. August 22, 2010 M4rc07 the "Thumbnails"

    [...] Followed this tutorial and it works http://www.undolog.com/2010/04/09/wordpress-2-9-la-nuova-funzione-post-thumbnails/ but the images (of the same articles ) I'm going to enter as thumbnail images are [...]

  17. September 1, 2010 Gianluca:

    . Hello congratulations for the blog, I have a question, I have a gallery that loads the images from the custom fields of posts, in theory I created a variable named $BG_image with the link to the picture type http://www.miosito7immagine/foto.jpg .
    How can I put in place the link in the photo evidence, if this transform

    1
    , $single = true ) ; ID, 'BG_image', $ single = true);

    in

    1
    get_the_post_thumbnail ( ) ; $ BG_image get_the_post_thumbnail = ();

    I'm not a php expert as you have already seen ...

  18. September 1, 2010 Giovambattista Fazioli :

    @ Gianluca: could you explain the problem better, I do not understand what you got. :)

  19. February 6, 2011 stefano:

    now I understand that I must not put the signs of opening and closing php, I apologize

    No ... I go .. what is wrong?
    I'm using a theme that has the timthumb.php but this does not work because I installed WPMU.

    In functions.php

    1
    2
    3
    ) ; add_theme_support ('post-thumbnails');
    , 64 , true ) ; set_post_thumbnail_size (64, 64, true);
    , 512 , 9999 ) ; add_image_size ('single-post-thumbnail', 512, 9999);

    (subito dopo get_header) perchè index mi da Templatepath. '/front.php' In index.php and front.php (after get_header) because it gives me index Templatepath. '/front.php' Templatepath. '/front.php'

    1
    the_post_thumbnail ();

    And in single.php (after get_header)

    1
    ) ; the_post_thumbnail ('single-post-thumbnail');

    this is done, things do not change

  20. March 7, 2011 Paul:

    Hello I can not change the size of the tumb. in the file function.php I write this:

    1
    , 117 , true ) ; set_post_thumbnail_size (190, 117, true);

    but does not change size.
    What should I do??

    thank you

    paul

  21. March 8, 2011 Giovambattista Fazioli :

    @ Paul: I do not work because the thumbnails you have already created. You must delete the file and reload it to cause it to be given the new setting. The thumbnail, in fact, are not generated runtime when the function is called, but when you create the first time in the backend, if you think WordPress would recreate "the fly" all the thumbnail for a site with 2000 articles ...
    That setting, we say, is not retroactive!

  22. March 15, 2011 Frederick :

    A hello to all.
    I tested the function creaGriglia() described above, but I do not work.
    I wish that every click on a category I come out of the thumb relative to the same category, or perhaps through add_image_size create a new dimension and put it into a beautiful gallery jquery.
    I could make a direct query to the database, I see where they are inserted, but there will be an easier way ...!
    Thanks in advance!

  23. March 15, 2011 Giovambattista Fazioli :

    @ Federico: I could not work for various reasons, eg you have not set the thumbnail, or does not have a category blog ... exactly what happens to you? Get some error message?

  24. March 15, 2011 Frederick :

    I ask forgiveness ... I do not have a category blog ...!
    Now it works perfectly, the personalizzerò integrating a gallery, I should be able to see how well it is possible to intervene in the function.
    Thank you and I renew my apology.

  25. March 16, 2011 Tiziana:

    Hello,
    thanks for your guide really useful. I wanted to ask is there a way to automatically fit the new option to the previous post where the thumbnails were gestitie via custom fields or do I manually add the thumbnails for all the old posts? I hope you can help me, thanks in advance all

    Tiziana

  26. March 16, 2011 Giovambattista Fazioli :

    @ Tiziana: You can do both. As mentioned in the article above, if you use a code like this:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    function_exists ( 'has_post_thumbnail' ) ) && ( has_post_thumbnail ( ) ) ) { if (( function_exists ('has_post_thumbnail')) && (has_post_thumbnail ())) {
    / / Wordpress 9.2 + using the embedded thumbnail
    the_post_thumbnail ();
    { Else {}
    / / Wordpress 2.8 and above - use the technique of the custom field
    / / Use a custom field called "field-thumbnail" or as
    / / You want that contains the URL of an image
    get_post_meta ( $post -> ID , 'campo-thumbnail' , true ) ; Postimage get_post_meta = $ ($ post -> ID, 'field-thumbnail', true);
    $postimage ) { if ($ postimage) {
    . $postimage . '" alt="" />' ; echo '<img src="'. $postimage.'" alt="" />';
    }
    }

    You can see the thumbnail of the new products and, if so, remove the address from the post-meta image of old articles, the ones that we used a meta tag.
    Also you can reapply to all the new features articles, read How to set automatic thumbnails of all posts

  27. March 16, 2011 Tiziana:

    Hello,
    But this code I've indicated automatically puts in the thumbnail of the homepage for the old post with custom fields that have? Thanks

  28. March 16, 2011 Tiziana:

    Hello I solved doing as written in the link you provided. Thanks

  29. March 18, 2011 Tiziana:

    Hello as said : ( . I inserted the code that I've mentioned on other blogs but it does not recognize the old miniatures (managed by custom fields). How can I fix this? I hope you can help me, thanks

    Tiziana

  30. March 24, 2011 Innobrain :

    Hello and congratulations for the great post ;)
    While we're on the subject I wanted to ask you something, I'm using the plugin "Category Posts" which allows you to add a widget with the posts that we want for each category, showing also the thumb of course related to the article for each class.
    The default plugin inserts a thumbnail after the name of the post, but I would like at the beginning and then the name of the post. How could I do?

  31. May 26, 2011 Alfredo :

    Hello,
    congratulations for the blog.
    I was able to trigger the display of thumbnails according to your instructions even though I view them in the post in spite function.php has inserted the line:

    1
    , array ( 'page' ) ) ; add_theme_support ('post-thumbnails', array ('page'));

    How do I avoid getting the thumb inside the post?

    Thanks
    Alfredo

  32. May 26, 2011 Giovambattista Fazioli :

    @ Alfredo: a possible cause could be that something has triggered the thumbnails on the post, as the line of code you provided is correct. To test you could place before add_theme_support :

    1
    ) ; remove_theme_support ('post-thumbnails');

    Let me know if it works? I am in the meantime I investigate.

  33. June 29, 2011 Jgor:

    Ok, everything works. Thanks to the availability 'entering into practice:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    function_exists ( 'has_post_thumbnail' ) ) && ( has_post_thumbnail ( ) ) ) { if (( function_exists ('has_post_thumbnail')) && (has_post_thumbnail ())) {
    / / Wordpress 9.2 + using the embedded thumbnail
    the_post_thumbnail ();
    { Else {}
    / / Wordpress 2.8 and above - use the technique of the custom field
    / / Use a custom field called "field-thumbnail" or the way you want it
    / / Contains the URL of an image
    get_post_meta ( $post -> ID , 'campo-thumbnail' , true ) ; Postimage get_post_meta = $ ($ post -> ID, 'field-thumbnail', true);
    $postimage ) { if ($ postimage) {
    ; echo'';
    }
    }

    I upgraded it more 'page themes. Now I ask you something.
    I need to put the image loaded the_post_thumbnail(); on a div, to put the text above.
    So I need to get only "src" and then pass it to the css.

    Thanks

  34. June 29, 2011 Giovambattista Fazioli :

    @ Jgor: you can do in various ways. To get the code (and not the output) you can use get_the_post_thumbnail() . This function, however, gives you back all the HTML markup.
    In this function, however, for information, the filter is linked post_thumbnail_html .
    che vuole come parametro l'ID del thumbanail. If you want to get just the image url, so to be included in the attribute src , you can use wp_get_attachment_thumb_url() that takes as a parameter the ID of thumbanail. The latter get through get_post_thumbnail_id() , as a parameter that takes the ID of the post.

  35. June 30, 2011 Jgor:

    @ Giovambattista Fazioli:
    Thank you! ^ _ ^
    congratulations for the work done. Have you counted a place in my rss-reader ^ _ ^

  36. September 13, 2011 vito:

    Hello, I use WP 3.2.1 installed with the theme Costelo. my problem 'that I can not delete thumbnail images, the thumbnails anyway ... when I go to click on the link "remove image out" ... I read the link and clicking action = edit # nothing happens ... same problem in trying to change the status of visibility 'and other little things but' I can change by editing faster ... I remain short, the problem of removing the thumbnails ... wich 'command should I use to remove the thumbnail (the link that I see Move your mouse over to "remove image"? I have to go in that file to restore the right code?
    Gazie

  37. November 10, 2011 Simona:

    Hello, I kindly need to know if these strings are used to trigger the display of thumbnails in the post, I do not understand anything of this language but I saw that the add_theme_support('post-thumbnails'); is present in my essay, in If your serve to place them exactly where I'm supposed previews? can you give me a hand?

    thanks :)

  38. November 10, 2011 Giovambattista Fazioli :

    @ Simon: the function adds specified by you (or skill if you prefer) the ability to set the thumbnail for each post, but by itself does not display anything. If I understand correctly:

    If these strings are used to trigger the display of thumbnails in the post

    to do this there are usually three ways:

    1) Use a theme that already permits
    2) Install a Plugin
    3) manually Act

    Excluding the first, you can solve by itself, if I wanted to automatically display the thumbnails (if present) in a post just add these lines of code to your file functions.php in the folder of your active theme:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    $content ) { showThumbnail function ($ content) {
    ; global $ post;

    $content ; Original_content $ = $ content;

    current_theme_supports ( 'post-thumbnails' ) ) { if (current_theme_supports ('post-thumbnails')) {
    the_post_thumbnail ( ) ; $ Content = the_post_thumbnail ();
    $original_content ; $ Content. = $ Original_content;
    }
    ; return $ content;
    }
    , 'showThumbnail' ) ; add_filter ('the_content', 'showThumbnail');

    Code like the above will find many online, according to your needs. I see you I created a simple as possible to keep them straight. I understand that if you're not a "technical" experience can be frustrating and tiring, better then a theme prefatto or use of a plugin. Hope that help ...

  39. November 10, 2011 Giovambattista Fazioli :

    @ Vito :: the link is correct, since the removal of the thumbnail is via JavaScript, here is an example:

    1
    "WPRemoveThumbnail('f46ef189fb');return false;" id = "remove-post-thumbnail" href = "#" rel = "nofollow" > Rimuovi immagine in evidenza < / a > < a onclick = "WPRemoveThumbnail ('f46ef189fb') return false;" id = "remove-post-thumbnail" href = "#" rel = "nofollow"> Remove picture out </ a >

    Probably, from what I understand, your problem is a malfunction of the issue, something hard to settle here ...

  40. November 10, 2011 Simona:

    @ Giovambattista Fazioli ::

    Thanks!! :) )))

    very very nice ... I'll try ... I do not work more than anything ;)

    Have a good day.

  41. November 24, 2011 WordPress snippet: src thumbnail - Undolog.com - Undolog.com :

    [...] Allows you to associate a thumbnail to a post, personalized page or post. If the entire image is easy to extract from the code, thanks [...]

  42. December 4, 2011 Joseph :

    Congratulations!
    Guide very interesting.

  43. December 4, 2011 Joseph :

    Council if you want to restore all the thumbs plugin Regenerate Thumbnails

  44. December 5, 2011 Joseph :

    Hi I wanted to ask you how you can help:
    in post I have uploaded a gallery of 50 images, for example, then the queue will be displayed in Article 50 thumbnails 100 × 100. I would like to figure out how to display only 5 in the article and click on one of these pictures I found on page 5, the existing (attachment.php), with the full gallery of all 50 images.
    Thanks

  45. December 29, 2011 kainone:

    But how can you create the thumb and attach it to the post as imamgine out?

  46. March 26, 2012 GM:

    Hi, sorry to resurrect an old post but I hope you can give me a hand. How do I, with this method, using images loaded via an external link http://www.sito.it/image.jpg ?

    For now I can only upload images from my hosting but I would not weigh too much space. How can I do?

    Thanks in advance

  47. March 29, 2012 Giovambattista Fazioli :

    @ GM: If you upload images to a different server (that WordPress can not do his) can set up a miniature sheet by selecting the source URL as shown in this post (start of article). In this way, WordPress will use an absolute address to deal with this miniature.
    Also, 'you lose' any downsizing of the same, you have to do on the remote server and / or acting through the CSS.

Leave a comment

TAG XHTML PERMITS: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> INSERTION CODE:
 <pre></pre> // blocco generico <code></code> // blocco generico [cc_actionscript][/cc_actionscript] // Actionscript [cc_actionscript3][/cc_actionscript3] // Actionscript 3 [cc_css][/cc_css] // CSS Style Sheet [cc_html][/cc_html] // HTML [cc_js][/cc_js] // Javascript [cc_objc][/cc_objc] // Objective-C [cc_php][/cc_objc] // PHP [cc_sql][/cc_sql] // SQL 


Stop SOPA