• Resolved sarahsas

    (@sarahsas)


    Hello! Just wondering if it’s possible to pull something other than the “featured image” for the related posts. For example in your Post Grid plugin, how you can change the setting to pull the first image from the post content instead of the featured image. Is that option possible in this plugin as well?

    Alternatively, is there a way using Post Grid or Post Grid Pro plugin to set a query to show “related posts” to the current page/post based on Category or Tag?

    Thanks so much in advance…

Viewing 2 replies - 1 through 2 (of 2 total)
  • Hi @sarahsas,
    Thanks for using our plugins.

    Unfortunately, this feature isn’t available in our Related Post plugin. But you can achieve this functionality with a little bit of custom coding.
    Just copy all the code from below, and from your WordPress dashboard, go to Appearance => Theme File Editor, and from the right-hand sidebar, select Theme Functions file. Now scroll down at the bottom of the file, paste the code, and update. Now the related post will pull up an image from your post content, not from the thumbnail. You can follow the screenshot: https://i.imgur.com/b7oBSkY.png

    Code:

    add_action( 'related_post_loop_item_element_post_thumb', 'remove_related_post_thumbb' );
    	function remove_related_post_thumbb() {
    		remove_action( 'related_post_loop_item_element_post_thumb', 'related_post_loop_item_element_post_thumb' );
    	}
    
        add_action('related_post_loop_item_element_post_thumb', 'related_post_loop_item_element_post_thumb_20200126', 10, 2);
       
       function related_post_loop_item_element_post_thumb_20200126($loop_post_id, $elementData)
       {
    	   $html_thumb = '';
    	   $thumb_linked = 'yes';
    	   $item_post_permalink = get_permalink($loop_post_id);
    	   $post = get_post($loop_post_id);
    	   $post_content = $post->post_content;
    	   $first_img = '';
    	   ob_start();
    	   ob_end_clean();
    	   $output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post_content, $matches);
       
    	   if (!empty($matches[1][0]))
    		   $first_img = isset($matches[1][0]) ? $matches[1][0] : '';
       
    	   if (empty($first_img)) {
    		   $html_thumb .= '';
    	   } else {
       
    		   if ($thumb_linked == 'yes') {
    			   $html_thumb .= '<a href="' . $item_post_permalink . '"><img src="' . $first_img . '" /></a>';
    		   } else {
    			   $html_thumb .= '<img src="' . $first_img . '" />';
    		   }
    	   }
       
    	   echo $html_thumb;
       }

    Let us know if it helps.
    Regards.

    Thread Starter sarahsas

    (@sarahsas)

    Wow, above and beyond, thank you so much for this!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Change featured image’ is closed to new replies.