【问题标题】:How to display thumbnails in Wordpress for related posts by author?如何在 Wordpress 中显示作者相关帖子的缩略图?
【发布时间】:2013-06-07 00:52:02
【问题描述】:

我有一个使用此代码的作者功能的相关帖子:

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

为了输出,我在 single.php 文件的循环中使用以下代码:

 <?php echo get_related_author_posts(); ?>

目前该功能仅将帖子标题显示为链接。

应如何查看此 wp 代码,以便按作者功能显示此相关帖子的缩略图?

【问题讨论】:

    标签: wordpress


    【解决方案1】:

    试试:

    foreach ( $authors_posts as $authors_post ) {
      $output .= get_the_post_thumbnail($authors_post->ID);
      $output .= '<a href="' . get_permalink( $authors_post->ID ) . '">' . apply_filters( 'the_title', $authors_post->post_title, $authors_post->ID ) . '</a>';
    }
    $output .= '</div>';
    

    查看get_the_post_thumbnail 了解更多信息,例如图像大小和额外类等属性。
    如果您需要更多帮助,请告诉我。

    【讨论】:

    • 只显示当前帖子缩略图(同一张图片),10次。我试过 $output .= get_the_post_thumbnail($authors_post->ID, 'related-thumb');但它只显示 8 个缩略图而不是 10 个。
    • 你确定所有这些帖子都有图片吗?如果你跳过'related-thumb' 参数怎么办?
    • 是的,我确定。 related-thumb 参数与缩略图大小有关。
    • 找到了。这是与插件有关的问题。非常感谢!
    • 我希望它不是我的插件 ;) wordpress.org/extend/plugins/default-featured-image
    猜你喜欢
    • 2017-08-02
    • 1970-01-01
    • 1970-01-01
    • 2012-02-26
    • 2015-12-04
    • 2020-06-08
    • 2016-11-21
    • 1970-01-01
    • 2011-03-15
    相关资源
    最近更新 更多