Support » Fixing WordPress » Recent posts in sidebar display current post's thumbnail
Recent posts in sidebar display current post's thumbnail
-
I’m trying to add a “Recent Posts” section to the sidebar that includes thumbnails. The links and recent posts do show, but instead of displaying their corresponding thumbnails, the current post’s featured image gets displayed. You can see it here: http://spanishmedia.com/eepfdev/?p=1080
Here’s the code I’m using as a PHP Text Box widget on the sidebar:
<?php foreach ( (array) get_posts('numberposts=5') as $_post ) : ?>
<div class="th fl">ID); ?>"><?php the_post_thumbnail ($_post->thumbnail, 'popular'); ?>
</div>
<div>ID); ?>"><?php echo $_post->post_title; ?></div>
<div class="hl2"></div>
<?php endforeach; ?>
Needless to say, any thoughts and suggestions on how to fix this problem would be greatly appreciated!
Carlos
-
try and change this section:
<?php the_post_thumbnail ($_post->thumbnail, 'popular'); ?>
into:
<?php echo get_the_post_thumbnail ($_post->ID, 'popular'); ?>
(not sure if ‘popular’ refers to a thumbnail size?)
http://codex.wordpress.org/Function_Reference/get_the_post_thumbnail
Pure awesomeness!! That did the trick indeed. Thank you for your help and for responding so quickly. BTW, you are right, “popular” is a thumbnail size.
Happy Sunday,
CarlosWhere is this “popular” defined?
Actually, would you also mind uploading your CSS that applies to this? Your site looks very crisp and clean, I love it.
Hi Derek,
Hmm, it’s been a while and can’t remember all the details, I’m sorry. I think “popular” was not a thumbnail size after all, but rather a parameter used in the code to query the images from the respective posts.
What formats the actual thumbnails is a class defined as “th” in the CSS stylesheet as follows:
.th { display: block; margin: 0 0 .5em; } .th.fl img { margin-right: 10px; } .th.fl img { width:40px; height:40px; border: 1px solid #cfcfcf; }
The text that accompanies the thumbnails (which would be the title of the posts) simply inherits the properties you define in the stylesheet for the sidebar text widgets, which is what I used.
Does this help?
Hi, thanks for the reply. This is great. I just need to figure out the “popular” part, as it does not seem to work for me. there must be some declaration that I need to do for it.
Ok, correction, while I’m not sure what popular does, this does work. However, it is calling the full sized image and then generating its own thumbnail, which can cause a considerable lag as they download, depending on the size of the original. I’m going to toy with this, but if you, or any other reader, happens to know how to augment the code above to call the thumbnail only, I’d love to hear how.
Thanks,
DerekOkay, got it figured out, sorry for the flurry of updates. I used this code:
<?php foreach ( (array) get_posts('numberposts=5') as $_post ) : ?> <div class="th fl"><?php echo get_the_post_thumbnail ($_post->ID, 'thumbnail'); ?> </div> <div><?php echo $_post->post_title; ?></div> <div class="hl2"></div> <?php endforeach; ?>
Note I’ve replaced ‘popular’ with ‘thumbnail’ and now true thumbnails are put in place, which default at 150×150. The 40×40 is generated using your CSS above.
Thanks again,
DerekGreat! The code I used is a carry over from older designs I’ve used over the years before WordPress supported automatic thumbnails, featured images, etc. out of the box. In other words, it’s pretty old.
Thank you for bringing it up to date and making it better!
Carlos
Thank you again! Very helpful! Derek
PS: You site is quite elegant and nice, great job!
- The topic ‘Recent posts in sidebar display current post's thumbnail’ is closed to new replies.