WordPress.org

Ready to get started?Download WordPress

Forums

WordPress Popular Posts
Using the Post Thumbnail Feature with WordPress Popular Posts (24 posts)

  1. Héctor Cabrera
    Member
    Plugin Author

    Posted 3 years ago #

    By default, WordPress Popular Posts will attempt to generate a thumbnail from the first image attached to your posts. However, you can also select a custom thumbnail by enabling The Post Thumbnail functionality on your blog. To do this, simply add this line of code to your theme's functions.php file:

    // Enable post thumbnails
    add_theme_support('post-thumbnails');

    Once you're done with that, go to your WordPress Admin interface and you should see a nex bow called Post Thumbnail (renamed "Featured Image" on WordPress 3.0) under the Add New Post / Edit Post screens. Pick the image of your choice, click on Use as thumbnail (changed to "Use as featured image" on WordPress 3.0) and save changes.

    Now WordPress Popular Posts will use your selected images as thumbnails!

  2. Héctor Cabrera
    Member
    Plugin Author

    Posted 3 years ago #

    Please note that this tutorial applies only for WordPress Popular Posts v2.1.0 and newer. Earlier versions don't support this feature.

  3. Mansur
    Member
    Posted 3 years ago #

    Thank you for new version plugin, work fine for me especially widget area.

    But how I can use this features at shortcode or template tag, manually generated using specified parameter.

    Thank You.

  4. Mansur
    Member
    Posted 3 years ago #

    I fixed the problem. All work fine.

    problem is my browser cache. I tried clear cache [CTRL] + [R]

    Thank you.

  5. Héctor Cabrera
    Member
    Plugin Author

    Posted 3 years ago #

    Good to know that, mansur!

  6. ma2t
    Member
    Posted 3 years ago #

    Thanks for the information, but what about when you're using the PHP code and not the widget?

    eg:

    <?php if (function_exists('wpp_get_mostpopular')) wpp_get_mostpopular("range=weekly&order_by=comments"); ?>

    Cheers!

  7. Héctor Cabrera
    Member
    Plugin Author

    Posted 3 years ago #

    Hi mat2t,

    It also applies to the PHP code. Here's an example:

    <?php
    if (function_exists('wpp_get_mostpopular')) wpp_get_mostpopular("range=weekly&order_by=comments&thumbnail_selection=usergenerated");
    ?>

    You can find more about this attribute and others on your wp-admin > Settings > WordPress Popular Posts page.

  8. ma2t
    Member
    Posted 3 years ago #

    Thanks a lot for your help Ikki24.

    I used your exact code in my sidebar, but I'm not seeing the thumbnails.

    It shows the title and number of comments, but not thumbnail.

    I have set a thumbnail (featured image in WP 3.0)

    I also checked the source, no sign of any images :/

    Thanks again.

  9. Héctor Cabrera
    Member
    Plugin Author

    Posted 3 years ago #

    Sorry ma2t, forgot to mention that you also need to specify the width & height of the thumbnails, like so:

    <?php
    if (function_exists('wpp_get_mostpopular')) wpp_get_mostpopular("range=weekly&order_by=comments&thumbnail_selection=usergenerated&thumbnail_width=30&thumbnail_height=30");
    ?>

    That should work.

  10. ma2t
    Member
    Posted 3 years ago #

    Ah cool thanks Ikki24 :)

    But unfortunately that's what I was trying to avoid...

    With the built in wordpress featured image, you can set it to not crop exactly. Mine is set to 50 px wide, and whatever high (bigger number). I always use landscape images so it's okay, but the height will vary a little each time

    Using the wordpress <?php the_post_thumbnail(); ?> code, it brings it in at the correct size. Without needing to specify a size.

    Having to specify a size will result in slightly squashed or stretched images.

    Oh well. Thanks for the help!

  11. ma2t
    Member
    Posted 3 years ago #

    Cool!

    Not sure if the latest update fixed this or not, but it seems to be working well now :)

    I used the code you gave me, but changed the width to 50, and height to 35, now even if my thumbnail is less than 35 in height, it still displays it correctly (values from WP), no stretching or squashing!

    Great news, thanks for the help.

  12. Héctor Cabrera
    Member
    Plugin Author

    Posted 3 years ago #

    Nope, haven't changed anything ma2t! ( at least, nothing related to the thumbnail feature anyways :P )

    If you set the thumbnail_source parameter to usergenerated (like the example code I posted above), then WPP will use the get_the_post_thumbnail() function and should handle everything (such as image cropping and rezising).

    I am, however, thinking of removing either the attribute width or height from the HTML that my plugin echoes to screen to avoid the image stretching effect.

  13. ma2t
    Member
    Posted 3 years ago #

    Oh! Well, as long as it's working :)

    Yeah, I feel it would be a good idea to allow the thumbnail (featured image) to show if just "thumbnail_selection=usergenerated" was used, and no need for the width or height.

    As I think these values can come from the wordpress code.

    Thanks for the great work!

  14. patung
    Member
    Posted 3 years ago #

    Hi, just wondering whether the generate thumbnails bit works with Multisite, as I've read that wpmu in the past had problems with timthumb script, it wouldn't work by default. So far I've played around with it and it hasn't been generating the thumbnails. I made the cache directory writeable btw.

  15. Héctor Cabrera
    Member
    Plugin Author

    Posted 3 years ago #

    Hi patung,

    To be perfectly honest with you, I had no idea my plugin even worked on MU till recently when someone told me it did.

    Anyways, back on topic. If you set WordPress Popular Posts to generate all the thumbnails for you, it'll use timthumb.php to do so. I've heard that MU has some issues with timthumb.php but again as I said before I had no idea people were using my plugin on MU blogs so I really didn't worry about that since I was developing thinking of standalone WordPress blogs and not MU. Can't tell how to fix this for MU blogs as I don't really know how MU works.

    On the other hand, if you set WPP to use the thumbnails you've selected under Featured Image then my plugin will use this instead and should be able to display a thumbnail. The timthumb.php script won't be used at all. You might want to try this approach instead, theoretically speaking it should work.

  16. patung
    Member
    Posted 3 years ago #

    Thanks Ikki, yes that's what I did, incidentally this is the post that people always bring up when wpmu and timthumb are discussed - http://www.binarymoon.co.uk/2009/10/timthumb-wordpress-mu/

  17. Héctor Cabrera
    Member
    Plugin Author

    Posted 3 years ago #

    Thanks for the link, patung.

    Since I don't have MU installed, you'll have to do the testing for me. Please open wordpress-popular-posts.php using an editor such as Windows' Notepad or Adobe Dreamweaver, and change this (line 706):

    $img = $this->get_img($wppost->ID);
    if ( ($img && !empty($img)) ) {
        $thumb = "<a href=\"".get_permalink($wppost->ID)."\" class=\"wppgen\" title=\"". $title_attr ."\"><img src=\"". $this->pluginDir . "/scripts/timthumb.php?src=". $img[1] ."&h=".$tbHeight."&w=".$tbWidth."&zc=1\" alt=\"".$title_attr."\" border=\"0\" class=\"wpp-thumbnail\" width=\"".$tbWidth."\" height=\"".$tbHeight."\" "."/></a>";
    }

    ... into this:

    $img = $this->get_img($wppost->ID);
    if ( ($img && !empty($img)) ) {
    	global $blog_id;
        if (isset($blog_id) && $blog_id > 0) {
            $imageParts = explode('/files/', $img[1]);
            if (isset($imageParts[1])) {
                $thumb = "<a href=\"".get_permalink($wppost->ID)."\" class=\"wppgen\" title=\"". $title_attr ."\"><img src=\"". $this->pluginDir . "/scripts/timthumb.php?src=". "/blogs.dir/" . $blog_id . "/files/" . $imageParts[1] ."&h=".$tbHeight."&w=".$tbWidth."&zc=1\" alt=\"".$title_attr."\" border=\"0\" class=\"wpp-thumbnail\" width=\"".$tbWidth."\" height=\"".$tbHeight."\" "."/></a>";
            }
        } else {
        	$thumb = "<a href=\"".get_permalink($wppost->ID)."\" class=\"wppgen\" title=\"". $title_attr ."\"><img src=\"". $this->pluginDir . "/scripts/timthumb.php?src=". $img[1] ."&h=".$tbHeight."&w=".$tbWidth."&zc=1\" alt=\"".$title_attr."\" border=\"0\" class=\"wpp-thumbnail\" width=\"".$tbWidth."\" height=\"".$tbHeight."\" "."/></a>";
        }
    }

    Let me know the results, ok?

  18. patung
    Member
    Posted 3 years ago #

    Brilliant! Works on domain mapped blogs too!

  19. patung
    Member
    Posted 3 years ago #

    Not sure if I'm doing something wrong here but on mainblog, blogid '1', I can't seem to get the post thumbnails generated, whereas as above your fix worked a charm for the sub-blogs. I just checked in the cache folder and there's definitely nothing in there from today, when I tried out the widget for the mainblog; there are a lot of other images in there though from the sub-blogs.

  20. Bike
    Member
    Posted 3 years ago #

    Hey Ikki24,

    is there a way the plugin can use YAPB generated thumbs? Like a 3rd option (besides usergenerated and wppgenerated)?

    On YAPB sites, the plugin does only show thumbs if the images are again uploaded/attached to the post (using the WP admin media uploader), something that already has been done by YAPB, so double work and double diskspace..
    If it can just use the Yapb cache?

    Cheers, Bike

  21. karimun
    Member
    Posted 3 years ago #

    Your script passes a truncated image name to timthumb.php if the name contains spaces (Should be the preg_match part).

    Here the changes I did in wordpress-popular-posts.php to make it work:

    Change line 795

    $img = wp_get_attachment_image($first_attachment->ID);
    preg_match('/<\s*img [^\>]*src\s*=\s*[\""\']?([^\""\'\s>]*)/i', $img, $imgm);

    into this
    $img = wp_get_attachment_image_src($first_attachment->ID);

    Change line 800
    return $imgm;
    into this
    return $img[0];

    And finally the path related code:

    In line 708 change
    timthumb.php?src=". $img[1]
    into this
    timthumb.php?src=". $img

    Thanks for your plugin.

  22. seriale
    Member
    Posted 2 years ago #

    Found a way to use "Category designated thumbnails" with this plugin .
    Lets say you want to use a specific category thumbnail for a post instead of "no_thumb.jpg", heres what you have to do :

    1. create a sub folder in the root of your wp installation (in this example the folder is "img" )

    2. name the images the same as the category name (if the category name is "30 Rock", use "30 Rock.jpg"

    3. if you wanna use the category thumbnail somewhere else in the site use this code
    <img src="<?php echo get_option('home'); ?>/img/<?php $category = get_the_category(); echo $category[0]->cat_name; ?>.jpg" width="120" height="120" alt="<?php the_title_attribute(); ?>" /></a> <?php }

    4. if you want to use the category thumbnail instead of no_thumb.jpg with this specific plugin edit "wordpress-popular-posts.php" ; look for this line

    // default image
    $thumb = "<a href=\"".get_permalink($wppost->ID)."\" class=\"wppnothumb\" title=\"". $title_attr ."\"><img src=\"". $this->pluginDir . "/no_thumb.jpg\" alt=\"".$title_attr."\" border=\"0\" class=\"wpp-thumbnail\" width=\"".$tbWidth."\" height=\"".$tbHeight."\" "."/></a>";

    and replace it with this

    // default image
    $category = get_the_category($wppost->ID) ;
    $thumb = "<a href=\"".get_permalink($wppost->ID)."\" class=\"wppnothumb\" title=\"". $title_attr ."\"><img src=\"". get_option('home') ."/img/". $category[0]->cat_name .".jpg\" alt=\"".$title_attr."\" border=\"0\" class=\"wpp-thumbnail\" width=\"".$tbWidth."\" height=\"".$tbHeight."\" "."/></a>";

    5. if all went well, when a post does not have a 'featured image' of a thumbnail cannot be generated, it will show the category picture instead

    ** if a post is in more the one category it will show the first one; there is a way to make this work better just check this http://new2wp.com/rookie/2-5-ways-to-show-category-thumbnails/

    Hope this helps... i would really like to see this integrated in the widget somehow

  23. echezuria
    Member
    Posted 2 years ago #

    If I enable the Post Thumbnail feature what happens with the old post where I did no specified a featured image yet? The plugin keeps generating a thumbnail for them?

    Thanks for your time

  24. echezuria
    Member
    Posted 2 years ago #

    Will be great if the next version comes with a customizable option to clean cache periodically.

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic