简体   繁体   中英

Add Thumbnail Image to all the posts by the same author in WordPress

I am trying to display all the posts by the same author on the single-author.php template. In the functions.php file I am trying to call the post title and permalink. However I am unable to add the post-feature image into the output. Any ideas would be helpful. Here is the functions.php code so far.

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' => 8 ) );

$output = '<ul>';
foreach ( $authors_posts as $authors_post ) {
    $url = wp_get_attachment_url( get_post_thumbnail_id($authors_posts->ID) );
    $output .= '<li style="background: url('. $url.')"><a href="' . get_permalink( $authors_post->ID ) . '">' . apply_filters( 'the_title', $authors_post->post_title,$authors_post->image, $authors_post->ID ) . '</a></li>';
}
$output .= '</ul>';

return $output;
}

尝试以下功能

 <?php echo get_the_post_thumbnail($authors_post->ID); ?>

Try this as suggested by @unixmiah

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' => 8 ) );

    $output = '<ul>';
    foreach ( $authors_posts as $authors_post ) {
      $url = get_the_post_thumbnail($authors_posts->ID,'post-thumbnail');
      $output .= '<li style="background: url('. $url.')"><a href="' . get_permalink( $authors_post->ID ) . '">' . apply_filters( 'the_title', $authors_post->post_title,$authors_post->image, $authors_post->ID ) . '</a></li>';
    }
    $output .= '</ul>';

    return $output;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM