Practical Uses for the Post-Thumbnail Function in WordPress 2.9
We are all used to seeing an image below blog posts titles, be it on blog homepages or on individual posts. However, until recently, adding these images was not as straightforward. Before the ability to register post thumbnails in themes was introduced in WordPress 2.9, WordPress theme developers were forced to use the post metadata in order to display an image associated with a post. In this tutorial, I’ll show you how to add this functionality to your themes and we will play around with some of the function options.
![]()
Practical Uses of WordPress Post-Thumbnail Function
Enabling the Post Thumbnail Functionality in your Themes
Enabling this functionality in your themes is actually so simple. All you need to do is add one line to your functions.php file (or two lines if you’d like a descriptive comment):
// Enable post thumbnails
add_theme_support('post-thumbnails');
Inserting post thumbnails
When you go to your WordPress admin panel and start writing a new post, you should see a new box on the right side of the page.
![]()
Please note that the Post Thumbnail block might not appear immediately below the Publish block, you might have to scroll down a little to see it. Clicking the Set Thumbnail link opens the Add an Image dialog.
![]()
Click on the Use as Thumbnail link and it will be added to your post:
![]()
Displaying the post thumbnails
After adding some dummy text to our post, we are ready to publish it. But before we preview the thumbnail, we’ll have to add a line of code to our WordPress loop. Open your index.php and add the following:
<?php the_post_thumbnail(); ?>
That will output an image tag with these classes: attachment-post-thumbnail and wp-post-image so that you can target your post thumbnails with CSS and style them to your liking. For this tutorial, I am using the theme I use for my website so there’s already a bit of styling applied, a border and some padding, nothing fancy.
![]()
Above is our post with the thumbnail displayed on the homepage. There is a problem, however, it is quite easy to resolve. My site uses a specific size for thumbnail images; it is 588 by 250 pixels. You can set this for your own WordPress blog by going to Settings > Media in your WordPress admin panel.
![]()
Another method you can use to make sure your thumbnails are the right size is to add their dimension to your functions.php file. To do this, we’ll add another line to our post thumbnail related code:
// Enable post thumbnails
add_theme_support('post-thumbnails');
set_post_thumbnail_size(588, 250, true);
Now we need to tell WordPress that it should display the image in this exact size. We’ll slightly modify the bit of code that was added to the WordPress loop in the index.php file:
<?php the_post_thumbnail('thumbnail'); ?>
WordPress now knows which version we want it to display in the loop and here is the improved result:
![]()
We will add the bit of PHP used for displaying thumbnails to our single.php as well so that we can see it when we open the posts.
![]()
That looks nice satisfactory, depending on your blog design, that could be all that you need to know about the post-thumbnail function. But we won’t stop here.
Adding more post thumbnail sizes
If you want your blog homepage to show post thumbnails in one size, and the posts to be displayed in a different thumbnail size, this is how you achieve it.
I will be using an image prepared in Photoshop; it’s a post thumbnail for one of my portfolio items. This bit about changing the thumbnail isn’t important for this tutorial so you can skip ahead to the part where we add more code to the PHP files.
The first thing to do is to remove the thumbnail that we’ve already assigned and add the new image as the post thumbnail. Removing the thumbnail is easy; just click the Remove Thumbnail link on the Edit post page in the WordPress admin:
![]()
You already know how to add a thumbnail so we won’t go into that again. Here is the homepage with the new thumbnail added:
![]()
The idea here is to use a smaller image for the homepage and the original-sized image for the posts. So we’ll need to create a new image size in our functions.php file.
add_image_size('loopThumb', 588, 125, true);
The arguments in the code above are:
- loopThumb – a handle assigned to our custom-sized images;
- 588 – image width in pixels;
- 125 – image height in pixels;
- true – a setting that tells WordPress whether to hard crop the image to reach the desired size.
We will need to alter the code in our index.php so that WordPress shows the new thumbnail size on the homepage:
<?php the_post_thumbnail('loopThumb'); ?>
![]()
I prefer this version because the slimmer images appear as teasers, as windows into the post thus inviting the visitor to open the post and continue reading.
Please note that changing the the_post_thumbnail call will not only alter the image size but also the CSS classes of the image. We now have these two classes for our image: attachment-loopThumb and wp-post-image. It might be safer to target the wp-post-image class in your CSS just to be on the safe side or add your own class when calling the thumbnails:
<?php the_post_thumbnail('loopThumb', array('class'=>'loopyThumbs')); ?>
That will output an img tag with the following classes: loopyThumbs and wp-post-image; quite useful.
If you want to display square images on the homepage, all you would need to do is to change the width and height in the add_image_size call:
add_image_size('squareThumb', 125, 125, true);
You’d probably want to float the attachment-post-thumbnail class to the left and add a right margin in your CSS:
.attachment-post-thumbnail {
float: left;
margin-right: 10px;
}
Now we change the code in index.php again so that the new square thumbnails will be displayed:
<?php the_post_thumbnail('squareThumb'); ?>
And that’s all we need to display the square thumbnails as seen in the screenshot below.
![]()
Well that’s not an exact square isn’t it? This is the problem you’ll run into if you add a new image size after you’ve already uploaded some images to your site. WordPress didn’t create the image size that you need so what you’re seeing is an image resized by the browser. Luckily there is a solution for this, a plugin called Regenerate Thumbnails. After you’ve added a new thumbnail size or changed any of the image sizes in the Media settings section of the admin panel, it will recreate the images into the necessary size.
After you install the plugin, you can regenerate all the images by going to Tools > Regen. Thumbnails and clicking the Regenerate all Thumbnails button. And now we’ll have properly squared images.
![]()
And that’s another problem solved.
Bulletproofing your Themes
Before WordPress 2.9 introduced post thumbnails, we had to rely on the post metadata also known as custom fields. Let’s say you’re building a WordPress theme that you plan on uploading to an online marketplace. You can only hope that people buying your theme will have blogs running the latest version of WordPress which has the post thumbnail support. However, there is a good chance that whoever purchases your theme has a blog running on an older version because they did not upgrade to the latest version of WordPress. There are a couple of simple if checks that will help us prepare the theme for all users.
First we want to check if the version of WordPress has the support for post thumbnails so here is the code we will add to the functions.php file:
if (function_exists('add_theme_support')) {
add_theme_support('post-thumbnails');
set_post_thumbnail_size(588, 250, true); // Normal post thumbnails
add_image_size('loopThumb', 588, 125, true);
}
We have wrapped our original code in an if check that verifies the new function exists and will only be called if it does exist.
Things get a little more complicated when we address the code that displays the post thumbnails. You’ll remember that was quite simple:
<?php the_post_thumbnail('loopThumb'); ?>
Now, we will want to check if there is a post thumbnail assigned to the post. If not, we will display an image that is set via the custom fields. And, as the final resort, if it can’t find anything, WordPress will display a default post image.
<?php
if (has_post_thumbnail()) {
the_post_thumbnail('loopThumb');
}
elseif (get_post_meta($post->ID, "Thumbnail", true) != '') { ?>
<img src="<?php echo get_post_meta($post->ID, "Thumbnail", true); ?>" alt="<?php the_title(); ?>" class="attachment-loopThumb wp-post-image" />
<?php }
else {
echo '<img src="images/defaultThumbnail.png" class="attachment-loopThumb wp-post-image" alt="Default Post Image" />';
}
?>
In the code above, the first if check verifies whether there is a post thumbnail. If there is one, it outputs the img tag with the post thumbnail. In case it doesn’t find a post thumbnail, we have a new check. This one looks for a custom field named Thumbnail which would be the URL to the post image. If one is found, it echoes out the img tag with the thumbnail image as its source.
Finally, if there is neither the post thumbnail nor the custom field with the thumbnail URL, we will echo out an img tag with a default post image.
Advantages of Using Post-Thumbnail over Get-Post-Meta
In closing, I want to cover a couple of benefits that you’ll gain from using the new post-thumbnail functionality. One of them is reducing MySQL queries. The post-thumbnail data is stored in the wp_post MySQL table which is normally called when you open a post. Earlier, when we had to rely on custom fields to insert post thumbnails, the browser would have to make a request to the wp_postmeta table, increasing the number of database queries.
I should also mention that there is an option to limit the post-thumbnail functionality to certain types of content. For example:
<?php add_theme_support('post-thumbnails', array('page')); ?>
The snippet of code shown above adds the post-thumbnail functionality only to pages and not to posts. WP Engineer recently shared their first impressions of Custom Post Type coming in WordPress 3.0 and it looks like we’ll (should the need arise) be able to add post thumbnails only to certain post types.
Imagine a music blog that would have posts with album reviews and posts regarding the site itself with which you do not wish to associate a thumbnail, it would be very easy to limit the thumbnail support only to reviews.
If you are running a blog on an older version of WordPress, I hope that this article has encouraged you towards upgrading. And if you’re worrying about creating new thumbnail image sizes, let me remind you of the Regenerate Thumbnails plugin.
Conclusion
I hope that you’ve enjoyed this article and that it has provided enough information on the post-thumbnail function. I hope that you’ve learned some new techniques and if you have any questions or suggestions, don’t hesitate to post them in the comment box below.
Fantastic post! You can't beat having all this information about Post Thumbnails in one place. I'm bookmarking this page for future reference and adding it to my RSS feed!
Ho yeah! Bookmarked for later, gotta give a look when i got time. Thnaks for that.
AWE-SUM! :9
Thanks guys! Glad you've found the article so useful. :)
Cheers, this is the most concise post-thumbnail article i´ve seen.
Thanks, Danny. Glad you like it. :D
You can use setting of the function the_post_thumbnail to specify any dimensions. This is handy if you want a different size on the Category page and Single one for exemple.
the_post_thumbnail(array(100,100));
That's true, but I felt that it was cleaner to use predefined, named sizes. :)
Thanks for pointing out another option, though. :)
How can you block the gallery from showing the thumbnail though since it's added as part of the page?
I'm not sure I understand the question. :( Could you elaborate, please?
When you do a post thumbnail in 2.9. or 3-alpha it includes it as part of the image array when you add more images to the post. So for example if I wanted to do a thumbnail for a product, then add 5 more images using the 'gallery' all six images will appear in the body of the post. Is t here an easier way to dismiss the thumbnail then passing the id of it to the gallery?
I'm still not sure if I understand completely but I believe that you don't want the thumbnail showing on the "product" page, right? If so, you could use a custom post template, say: product.php. And within it, you could omit the the_post_thumbnail() call.
I hope that this helps.
ok, try again and see if I can explain it. Maybe this was changed on 2.9.2 and I'm missing it somewhere.
I insert the thumbnail as normal. Now when I add new images (using the flash uploader) and move to the gallery tab and insert it into the post, the thumbnail is attached to that gallery, so it will appear twice on the page. Once as a thumbnail, the second as part of the gallery that WordPress generates. When you add the thumbnail to your post it sets it up as the first attachment to the post.
Here is an example: http://soldiersofliberty.org/2010/03/19/attachment-test/
The "thumbnail" appears where it should above page menu, but also part of the gallery that wordpress generates as well.
Hi Daniel! Sorry for not replying earlier and sorry for not realizing what you were trying to achieve.
I am afraid that the only sure way of excluding certain images from the gallery is adding the following to your gallery shortcode (like you've said):
[gallery exclude="IMAGE_ID" link="file"]
There is the Multiple Galleries plugin: http://konstruktors.com/blog/projects-services/wordpress-plugins/multiple-galleries/ but some people have had trouble using it so I can't really recommend it.
Sorry for all the confusion and I'm sorry that I can't offer a better solution to your problem. :(
Well, there are many thumbnail tutorials for WP2.9 out there, but this is a quite complete one. It's really worth for a bookmark. Well done Marko!
Thanks for the kind words, Lam. Appreciate it! :)
Couldn't agree more with you!
I had been browsing some of the other tuts out there and they didn't quite cut it.
Now I can sit back and know that the imgs I use in the custom themes I build, will always be the correct size and not have to fall back on css to size correctly.
Thank you
You're welcome and thanks for the kind words. It's really great to hear that the article is so helpful. :)
Cheers!
Wahoo this is THE ultimate post thumbnail tuts !
Thx for sharing :-)
You're welcome! Glad you liked it so much. :D
Often when I add a thumbnail and save the post I see that the thumbnail is not saved with the post at all.
I then have to add the thumbnail again and save the post again.
The second time it always works.
Anyone else run into this?
First time I hear about that problem, sorry. :( It always works fine for me.
BTW, love the art on your site. :)
I'm from Malaysia and this is really one of the best WordPress tutorial of this sort that I have seen. Marko, please continued to write more of this type of great tutorial and I will surely share with my friends.
Thank you so much Marko and onextrapixel!
You're very kind, Nasri. :) Thank you for the support. The topic of my next article won't be WordPress but I believe that the article will be useful to people using WordPress too. ;) Stay tuned!
Hi,
I am using WordPress MU 2.8.6 and i can't implement this feature. can you tell me what it's diferent width WPMu?
Regards
Daniela
Hi Daniela,
You are unable to use post thumbnails in your WordPress installation not because it is the multi-user version but because the version number is lower than 2.9. The post thumbnails functionality was added in WordPress 2.9 so you would have to update your WordPress installation to the latest version (currently, 2.9.2) in order to use the post thumbnails functionality.
I hope this helps you. :)
Hi there... I loved the article and I am learning more about WP all of the time but I have two questions please. First, is there a way to control the "quality" of the jpeg (such as 95% compression, let's say)? I know some scripts like Timthumb has a setting for this. Also, can the "lightbox" effect be used on the single post page? I like the original post showing a cropped because it kind of presents a teaser... but when you get to the full post it would be nice to have that larger thumbnail display in the lightbox.
Thanks for the input, hope you can help me out!
Hi Randy,
I'm glad you enjoyed the article and let's see if I can answer your questions. :)
Sadly, I haven't found a way to control the level of JPEG compression from the WP backend and it is unfortunate because those JPEG artifacts at high compression levels really are... undesirable, to say the least.
There are a couple of ways you can go about solving this. One is to use PNG images for the thumbnails and then you won't have to worry about quality, only image sizes. :) You can shave some kilobytes off the PNG image sizes using SmushIt (http://www.smushit.com/ysmush.it/) or PunyPNG (http://www.gracepointafterfive.com/punypng/).
You could also try the SmushIt WP plugin: http://wordpress.org/extend/plugins/wp-smushit/. It's supposed to optimize the JPEG compression of every image you add to a post, page, etc. I have tried it but I'm not sure that the resulting images have noticeably higher quality. Maybe it will work with your images. ;)
Finally, you can try using TimThumb alongside post-thumbnail. Let's use my example: default thumbnail size is 588x250px and that would be the only size we will set in functions.php. Then, in your index.php add the following:
< ?php $thumbURL = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'thumbnail' ); ?>img src="< ?php echo $thumbURL[0]; ?>" alt="Post Thumbnail" class="attachment-loopThumb wp-post-image"You control the image quality by the "q=XX" parameter, but you probably knew that already. :)
I am not sure I understand the second question. Could you elaborate on what effect you are trying to achieve?
Thanks for your comment Randy. I hope that I have been able to help you with one of your problems and I'll see if I can assist you further with the other one when you reply. :)
Marko, thank you so much for the input. I may explore those possibilities. On the JPEG quality item... my theme does have TimThumb built in. I just hoped to use the new WP feature as a convenience -and- it is a little simpler for others that I assist with on other sites. Just a note, the TimThumb script that my theme utilizes does allow for control of the compression.
On the second item... basically when you get to a full post page, I would like for the larger post thumbnail open up the "full image version" in a lightbox when clicked. Currently, it just points at the post page so it just loops, so to speak. Here is an example of that:
http://www.pocait.org/evangelism/ministry-to-the-deaf/
Thanks for your help... if you can give me further assistance, I would greatly appreciate it.
You're welcome, Randy. I must apologize for the delayed reply, had some trouble with displaying that PHP code snippet.
I now understand what you had in mind for the second item and I think that we can accomplish that. :)
The image in the post that you've shared a link to is 320x240 pixels and let's say that you want the bigger image to be 640x480 px. First, you would add another image size in your functions.php file:
add_image_size('lightImg', 640, 480, true);That way, we can link to that image size in single.php. Most lightBox scripts function by wrapping a thumbnail image in a link that points to the larger image so I'll assume that is the case here. Right now, your images on single posts are linked to the post's permalink, but we'll change it to point to the larger image:
< ? php $lightURL = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'lightImg' ); ?>a href="< ? php echo $lightURL[0]; ?>< ?php the_post_thumbnail('thumbnail'); ?>
To ensure that all of your older posts display larger images when you click on the post image, you should use the Regenerate Thumbnails plugin mentioned in the post.
I hope that this helps. :)
Trouble with the PHP again. Sorry about that, it will be solved soon...
Man, no problem at all... I just appreciate the help. I am on the way to a meeting now but later I will give this a try and let you know the results... Thanks again!
Thank you for being so understanding.
Looking forward to hearing if the results were satisfactory. :)
Marko, I think that this may be a little more complicated than I first thought because of how my theme is structured. It is an "Elegant Themes" theme and some of the files such as "functions.php" are different that what is probably standard... it makes calls to other files in their unique control panel. So, I am not sure that I can mess with the code too much without breaking some of it's other functionality.
I have emailed them and started a thread on their forum to see if I can get an answer from them. If I do, I will try to put a synopsis here for others who may find this topic. Thanks again...
Randy
Hi Randy, thank for posting the update. :)
I completely understand your reluctance to mess around with the theme files and I hope that the people at Elegant Themes will help you out.
Ok, I finally got this working as I hoped. A fellow at the Elegant Themes support forum steered me to the proper file and code. The forum is member only, sorry but I will assume that if you have one of their themes you will be able to see this post to view the fix: http://www.elegantthemes.com/forum/viewtopic.php?f=33&t=12042
Best regards to all and Marko, thank you again for your willingness to help. Have a blessed day.
That's great to hear, Randy. I'm glad you now have a theme with the functionality you need. :)
You're quite welcome and if there is anything else I could help you with... :)
Hi ,
Great article! But I am having trouble getting the thumbnail function to work! I can get everything to work in the dashboard when creating a new post but as soon as I click publish the browser window reloads blank.
It also affects other actions in the dashboard like filtering categories or moving post to trash. When I click to do them it just reloads to a blank page?
If I take the thumbnail function out it all works fine again!?
Thank you very much for your help
Hi Andy,
I'm sorry you are having trouble implementing the thumbnails into your theme.
I have to ask the most usual support question: which version of WordPress are you running? :) Without seeing your code, I'm afraid I can't do much more. Maybe you have left out a semicolon or forgot to close PHP tags somewhere and that breaks everything. Could you post the relevant code from functions.php and index.php? That might help us figure out the problem.
Thanks for the rapid reply! I'm using WordPress 2.9.2. The page I'm trying to use it on is not index.php but another page template showing just one category.
functions.php -
blog.php
<div class="home-post" id="post-">
<a href="" rel="bookmark" title="">
<a href="">ID, $key, true); ?>
Thank you
Sorry that hasn't shown correctly!
How should I write it so that it displays all the code?
Thanks
Yeah, I have just become aware of that problem a few hours ago. Try adding a space before the question mark when you open the PHP tags, like this:
That should work. :)
Sorry about that, still not working. We'll resolve the problem as soon as possible and get back to you.
Could you suggest a good theme that this would definitely work on so I can just test it to see how it works in the mean time!?
Thanks
Well, you can try it out with the WordPress default theme by Michael Heilemann that comes preinstalled with every WordPress installation. :)
Amazing! Thanks for the code mate
Hey Marko,
Great post. I'm wrestling with this seemingly simple coding issue involving wp thumbnails in the Mystique theme.
// set up post thumnails
if (function_exists('add_theme_support')):
add_theme_support('post-thumbnails');
$size = explode('x',get_mystique_option('post_thumb'));
set_post_thumbnail_size($size[0],$size[1], true);
add_image_size('featured-thumbnail', 150, 150);
I want the titles I'm using for each image thumbnail to be used as the alt attribute. Adding the following code at the end of the snippet above doesn't seem to work.
the_post_thumbnail( array( 'alt' => ''));
Is there something I'm missing?
Thanks for any help,
Dustin
Hi Dustin,
I am guessing that the value you want assigned to the
altattribute got parsed by WordPress' comment system here but, anyway, you should add thethe_post_thumbnailcall to your index.php or single.php, or wherever you want the thumbnail to appear. If I understood you correctly, you've been adding thethe_post_thumbnailcall to your functions.php file?I hope this helps you, if not, let me know and I'll see if I can offer further assistance. ;)
Thanks for your quick response and trying to help me with this one Marko b/c I'm totally stumped. I've tried contacting the theme developer or anyone on his site and haven't heard back yet.
I added the following code based on your suggestion to my index.php file and it didn't work.
the_post_thumbnail( array( 'alt' => ''));
The thumbnails show up on the main page next to each post and in the featured content slider. What I'm trying to do is duplicate the title for each post (or at the very lease get the alt descriptions from each image file) for SEO purposes.
Thanks again,
Dustin
You're welcome and let's see if we can make it work as you wish it to work. :)
The comments system has, again, parsed your code but I guess that you had either
theTitleAttribute()or theTitle() between the quotemarks. (I've used camelCase hoping that it won't get parsed.)You can try something like this:
thePostThumbnail(array('alt' => ''.getTheTitle().''));You should replace every capital letter with an underscore and the same letter, only lowercase.
This worked for me. If there is any other trouble, let me know if I can contact you via email to try and sort this out. :)
Great article, i'll use the info for our next theme.
Continue le bon travail!
Thanks, Simon. I'm glad the article will be useful to you. :)
This is like to have EVERYTHING in one place! Amazing work Marko!!
Just one question..(might be a dumb one...but I started using wordpress this week). I have the thumbnail appearing in my front-page as I wanted, and since this is for my portfolio the thumbnail reflets the last post i made with my work. What I want, since I will show only the thumbnail in the front-page, is how to link the thumbnail to the respective post. As it is now, the thumbnail is just an image... no link to anywhere...
Any thoughts???
Thanks and once again, amazing article!!
Hi Goncalo, you're quite welcome.
You can link a thumbnail to its respective post easily, just enclose the
the_post_thumbnailcall with a link like below:a href="< ?php the_permalink() ?>" rel="bookmark" title="< ?php the_title_attribute(); ?>">< ?php the_post_thumbnail(); ?>Let me know if you have any problems with this code.
Now that's what i call a proper article with well laid architecture and helpful tips. I must thank you for your efforts.
I have one questions though. I am using phpthumb to generate thumbnails on my site but now i am looking to switch to inbuilt wordpress thumbnail mechanism. would there be any performance related issues involved? I am going to use WP Super cache along with it and hoping those plugins won't conflict.
What do you suggest?
You're quite welcome, Katie. :)
I believe that there won't be any performance issues or conflicts. Is your plan to use the built-in thumbnail system for new posts only? If so, you can add a check to see whether a WordPress thumbnail is available. If there is one, display it, if not, execute the code you already use. You have the code to make such a check in the article.
If I can be of more help, let me know.
Thanks for your comment and I hope you won't run into any trouble implementing the new functionality.
Hi Marko,
You have a great tutorial for post thumbs in wp! cheers.
I was wondering If you tested the extra thumbnail size in 3.0 beta 1.
Ie: add_image_size('squareThumb', 125, 125, true);
It seems like it's breaking the featured thumbnail all together... let me know if you run into the same issue.
using the new twentyten theme...
cheers!
Hi,
You're welcome. :)
I haven't tested the new beta before, but I have just now and it works fine for me. Even with custom sizes added. I am not sure what might be breaking it for you. Have you tried it on a fresh install of WP 3.0b?
well it's almost virgin....
The thumbnail get created but it seems like when I want to set the post thumbnail the option isn't there anymore....
must be something i did in the twentyten theme.... I should have started from scratch like usual....
i'll keep you posted if I find the problem.
thx again.
Awesome article. Very detailed and easy to understand. Thanks.
Very usefull ! i'm think i use it in further projects, thanks for sharing !
Hi
Does this even work with for the signature theme ( that theme writes the code for you) Also
is there a way to make these thumbnails into picture links?
Hi Dionne,
I don't have any experience with the Signature Theme so I can't tell you if it supports the post thumbnail functionality. I have checked out their site and I haven't been able to find any info on it. I am sure that they will let you know if their theme supports it if you contact them via the support form on their website.
Regarding using thumbnails as links, you can easily accomplish that by simply wrapping the
the_post_thumbnailcall in aatag and enter the link as the value of itshrefattribute.I'm sorry I couldn't be of more help...
Marko, this is SOLID GOLD!
Thank you so much for sharing your knowledge
Greetings from Argentina!
My pleasure, Gabriel. I'm glad you have found the article so useful. :)
Hi Marko,
I saw that Dustin had kind of the same question already but I want to get the title attribute of the image that I use as thumbnail for the alt attribute as well.
getTheTitle(); will not work here because the post has another title than the image that I use as thumbnail.
It would be great if you could point me in the right direction.
Thanks in advance.
Have a good one.
Daniel
Hi Daniel,
In my experience, simply using the
the_post_thumbnail();call should make the WordPress output the thumbnail image with thealtandtitleattributes that you assigned while adding the thumbnail to the post. Maybe there is an issue with your WordPress install preventing the thumbnail functionality from working properly... :SI have found a kind of a workaround for this: you could try using the
get_the_post_thumbnail();template tag to output the thumbnail. It will allow you to pull the post's excerpt as thealtortitleattribute. Now, I don't know how your site is designed and how (or if) you use excerpts but maybe you can use this option to achieve the "effect" you desire. Here is the link from the WordPress Codex explaining the usage of theget_the_post_thumbnail();template tag: http://codex.wordpress.org/Template_Tags/get_the_post_thumbnail.I hope that you'll be able to make this work for you and if I can be of more help, let me know. ;)
Is there an easy way to assign a certain thumbnail image to a post based on the category selected.
Hi Michael,
I think that the easiest way to have a single thumbnail for all posts in a certain category is to add the thumbnail image manually in the PHP files (single.php, index.php...). You can to see if the post is in a certain category by using the
if (in_category('ID'))check. Just be sure to add theattachment-post-thumbnailandwp-post-imageclasses to the image.I can think of another solution but it's more complicated. :(
Let me know if this works for you.
Does WordPress 3.0 support the same function as 2.9.2?
3.0 supports the thumbnails functionality and some cool new features. I have upgraded my site to 3.0 and the thumbnails work without any problems. :)
Does anybody know how I can retrieve the image alt text . I need to display this in a span (The Originally Entered Alt Text) ::: Any Help would be appreciated
Hi, sorry for the late reply.
Does it have to be the
alttext or can you also use the text entered as the image'stitle? Check out my correspondence with Dustin in the comments above for more info.This was a very, very nice post; thank you very much for it!
Cheers
Hi, thanks for the article. I'm running wordpress 3.0 with some custom post types and I can't get the thumbnails to appear in the admin menu with:
add_theme_support( 'post-thumbnails');
I don't necessarily want to do it globally for all post types but either way it just doesn't seem to work at all. I'm hoping to figure out how to add 2 thumbnails per "Before and After" post type, where one is the before image and the next is the after image. This is to pass on to a client who is a cosmetic surgeon to be able to have his staff just input before and afters as post thumbnails in the post type.
Any reason why post thumbnails wouldn't be working in wordpress 3 with custom post types? I don't have any plug ins or anything going on.
Hi Michael,
I am running WP 3.0 on my site and I haven't run into any problems with post thumbnails. I am not using custom post types but you say that it doesn't work at all for you? I can only suggest that you check for a typo in the functions.php file for your theme. :)