随机缩略图 WordPress



我正在尝试在Wordpress上每个帖子的末尾加载4个随机缩略图,每个缩略图都应链接到其各自的帖子。我不知道如何正确地做到这一点,因为我对PHP知之甚少。我在主题中找到了获取缩略图和标题的代码片段:

<div class="col-sm-4 col-md-4 col-lg-4 tiles" id="post-<?php the_ID(); ?>" <?php post_class( 'post' ); ?> itemscope="" itemtype="http://schema.org/BlogPosting">
<?php
if ( has_post_thumbnail() ) {
echo '<div class="post-thumbnail">';
echo '<a href="' . esc_url( get_permalink() ) . '">';
echo get_the_post_thumbnail( $post->ID, 'shop_isle_blog_image_size' );
echo '</a>';
echo '</div>';
}
?>
<div class="tile_title">
<a class="post-title"><a href="<?php echo esc_url( get_permalink() ); ?>"><?php the_title(); ?></a></a>
</div>
</div>

此外,缩略图应与当前帖子来自同一类别。 例如,如果有人正在阅读"旅行"类别的博客文章,则 4 个缩略图应该只来自此类别,其余类别也是如此。可以这样做吗?

这称为"相关帖子"功能。有很多插件,例如YARPP就是这样一个插件。

如果你想避免插件,你可以在模板本身中获得具有此功能的主题。例如,我的博客使用功能区主题并包含此代码。

最新更新