• Resolved JRadagast

    (@jradagast)


    So, as explained in the title, i want the thumbnail to get the featured image, and if it doesn’t exists, to get the first on post. I searched everywhere and couldn’t find nothing.

    i’m setting the plugin up using this on my custom made theme. (i’m developing it from scratch).

    <?php if (function_exists('wpp_get_mostpopular')) wpp_get_mostpopular("post_html='<div>{thumb}<h2>{title}</h2></div>'&limit=5&range=monthly&order_by=avg&post_type=post&cat=-6&thumbnail_width=150&thumbnail_height=150&stats_comments=0"); ?>

    any help is apreciated.

    https://wordpress.org/plugins/wordpress-popular-posts/

Viewing 9 replies - 1 through 9 (of 9 total)
  • Plugin Author Hector Cabrera

    (@hcabrera)

    Hi JRadagast,

    Try this:

    1. Open wordpress-popular-posts.php using a text editor (or edit it directly from Plugins > Editor > WordPress Popular Posts).
    2. Around line 1199 you’ll find the relevant code block that builds the thumbnail. Replace it completely with the following:
      // POST THUMBNAIL
      if ($instance['thumbnail']['active'] && $this->thumb) {
      
      	$tbWidth = $instance['thumbnail']['width'];
      	$tbHeight = $instance['thumbnail']['height'];
      
      	$thumb = "<a href=\"". $permalink ."\" title=\"{$title}\" target=\"".$this->user_ops['tools']['link']['target']."\">";
      
      	if (function_exists('get_the_post_thumbnail') && has_post_thumbnail( $p->id )) { // check if the user has selected a post thumbnail
      		$thumb .= get_the_post_thumbnail($p->id, array($tbWidth, $tbHeight), array('class' => 'wpp-thumbnail', 'alt' => esc_attr($title), 'title' => esc_attr($title)) );
      	} else { // get first image from post
      		$thumb .= $this->get_img( $p->id, array($tbWidth, $tbHeight), 'first_image' );
      	}
      
      	$thumb .= "</a>";
      }
    3. Save changes.

    I have not tested this, but it should work.

    Keep in mind that this is a modification (hack) and will get overwritten once a new version of the plugin is available.

    Thread Starter JRadagast

    (@jradagast)

    Thanks man, it worked.

    I’ll save this change somewhere just to remember.
    Great plugin btw.

    Héctor, is this code block around line 1199 yet? Thanks.

    Plugin Author Hector Cabrera

    (@hcabrera)

    Hi Fernando,

    Nope, the plugin has changed a lot since then. Now you’d need to make this change on the __render_popular_post() function, to be more precise you’ll have to replace one variable (around line 1687):

    $thumb = $this->_get_thumb($p, $instance);

    … with the hack posted above. Also, $this->user_ops is now $this->user_settings.

    Can you add this as a new feature in the next update, because why not?

    Plugin Author Hector Cabrera

    (@hcabrera)

    I’ll think about it, but no promises.

    Hello. I can’t seem to make this change work. I’m just supposed to delete this line
    $thumb = $this->_get_thumb($p, $instance);
    and add this code, right?

    if ($instance['thumbnail']['active'] && $this->thumb) {
    				$tbWidth = $instance['thumbnail']['width'];
    				$tbHeight = $instance['thumbnail']['height'];
    
    				$thumb = "<a href=\"". $permalink ."\" title=\"{$title}\" target=\"".$this->user_settings['tools']['link']['target']."\">";
    
    				if (function_exists('get_the_post_thumbnail') && has_post_thumbnail( $p->id )) { // check if the user has selected a post thumbnail
    					$thumb .= get_the_post_thumbnail($p->id, array($tbWidth, $tbHeight), array('class' => 'wpp-thumbnail', 'alt' => esc_attr($title), 'title' => esc_attr($title)) );
    				} else { // get first image from post
    					$thumb .= $this->get_img( $p->id, array($tbWidth, $tbHeight), 'first_image' );
    				}
    
    				$thumb .= "</a>";
    			}

    I really don’t know what I’m doing wrong… But everytime I change it, the thumbnail disappears completely – no featured image, no first image from the post, nothing. Help?

    Plugin Author Hector Cabrera

    (@hcabrera)

    Ah, sorry about that. There are additional changes to be made I didn’t notice before. Here’s the updated code (didn’t test it though, so let me know how it goes):

    if ( $instance['thumbnail']['active'] && $this->thumbnailing ) {
    	$tbWidth = $instance['thumbnail']['width'];
    	$tbHeight = $instance['thumbnail']['height'];
    
    	$thumb = "<a href=\"". $permalink ."\" title=\"{$title}\" target=\"".$this->user_settings['tools']['link']['target']."\">";
    
    	if (function_exists('get_the_post_thumbnail') && has_post_thumbnail( $p->id )) { // check if the user has selected a post thumbnail
    		$thumb .= get_the_post_thumbnail($p->id, array($tbWidth, $tbHeight), array('class' => 'wpp-thumbnail', 'alt' => esc_attr($title), 'title' => esc_attr($title)) );
    	} else { // get first image from post
    		$thumb .= $this->__get_img( null, $p->id, null, array($tbWidth, $tbHeight), 'first_image', $title );
    	}
    
    	$thumb .= "</a>";
    }

    Now it works perfectly! Thank you!

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘I want the thumbnail to get either the featured image or the first in post.’ is closed to new replies.