WordPress.org

Forums

How to display thumbnails for related posts by author? (2 posts)

  1. mascatu
    Member
    Posted 2 years ago #

    I try to get related posts by author using this code:

    function get_related_author_posts() {
        global $authordata, $post;
    
        $authors_posts = get_posts( array( 'author' => $authordata->ID, 'post__not_in' => array( $post->ID ), 'posts_per_page' => 10 ) );
    
        $output = '<div class="block">';
        foreach ( $authors_posts as $authors_post ) {
            $output .= '<a href="' . get_permalink( $authors_post->ID ) . '">' . apply_filters( 'the_title', $authors_post->post_title, $authors_post->ID ) . '</a>';
        }
        $output .= '</div>';
    
        return $output;
    }

    For output I use the following code inside the loop of my single.php file:

    <?php echo get_related_author_posts(); ?>
    Currently it displays only post titles as links.

    How should look this code in order to display thumbnails for this related posts by author function?

    Thanks.

  2. Girmay
    Member
    Posted 2 years ago #

    This should do it.

    function get_related_author_posts() {
        global $authordata, $post;
    
        $authors_posts = get_posts( array( 'author' => $authordata->ID, 'post__not_in' => array( $post->ID ), 'posts_per_page' => 10 ) );
    
        $output = '<div class="block">';
        foreach ( $authors_posts as $authors_post ) {
           $img = featured_img($authors_post->ID);
            $output .= '<img src="'.$img.'"><a href="' . get_permalink( $authors_post->ID ) . '">' . apply_filters( 'the_title', $authors_post->post_title, $authors_post->ID ) . '</a>';
        }
        $output .= '</div>';
    
        return $output;
    }
    function featured_img($id){
    	$thumb = get_post_meta($id,'THUMB',true);
    	if($thumb){
    	return $thumb;
    	} else {
    		return "default thumb URL";
    	}
    }

Topic Closed

This topic has been closed to new replies.

About this Topic