WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Recent posts in sidebar display current post's thumbnail (11 posts)

  1. spanishcarlos
    Member
    Posted 3 years ago #

    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

  2. alchymyth
    The Sweeper & Moderator
    Posted 3 years ago #

    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

  3. spanishcarlos
    Member
    Posted 3 years ago #

    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,
    Carlos

  4. derekbeck
    Member
    Posted 2 years ago #

    Where is this "popular" defined?

  5. derekbeck
    Member
    Posted 2 years ago #

    Actually, would you also mind uploading your CSS that applies to this? Your site looks very crisp and clean, I love it.

  6. spanishcarlos
    Member
    Posted 2 years ago #

    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?

  7. derekbeck
    Member
    Posted 2 years ago #

    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.

  8. derekbeck
    Member
    Posted 2 years ago #

    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,
    Derek

  9. derekbeck
    Member
    Posted 2 years ago #

    Okay, 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 150x150. The 40x40 is generated using your CSS above.

    Thanks again,
    Derek

  10. spanishcarlos
    Member
    Posted 2 years ago #

    Great! 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

  11. derekbeck
    Member
    Posted 2 years ago #

    Thank you again! Very helpful! Derek

    PS: You site is quite elegant and nice, great job!

Topic Closed

This topic has been closed to new replies.

About this Topic