将缩略图添加到同一作者在WordPress中的所有帖子中

时间:2018-09-10 19:45:36

标签: php wordpress advanced-custom-fields

我正在尝试在single-author.php模板上显示同一作者的所有帖子。在functions.php文件中,我试图调用帖子标题和永久链接。但是,我无法将后功能图像添加到输出中。任何想法都会有所帮助。这是到目前为止的functions.php代码。

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;
}

2 个答案:

答案 0 :(得分:0)

尝试使用下面的功能

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

答案 1 :(得分:0)

按照@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;
}