Products
GG网络技术分享 2025-03-18 16:12 2
I\'m trying to load 4 random thumbnails at the end of each post on Wordpress and each thumbnail should link to its respective post. I can\'t figure out how to do it properly, since I have very little knowledge of PHP. I have found this code snippet in the theme that get the thumbnail and the title:
<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>
Furthermore, the thumbnails should come from the same category as the current post. E.g. if someone is reading a blogpost from the \"Travel\" category the 4 thumbnails should only come from this category, and the same goes for the rest of the categories. Is it possible to do this?
图片转代码服务由CSDN问答提供
感谢您的意见,我们尽快改进~
功能建议我正在尝试在Wordpress上的每个帖子的末尾加载4个随机缩略图,每个缩略图应该链接到它 各自的帖子。 我无法弄清楚如何正确地做到这一点,因为我对PHP知之甚少。 我在主题中找到了获取缩略图和标题的代码片段:</ p>
&lt; div class =“col-sm-4 col-md-4 col-lg -4个tile“id =”post-&lt;?php the_ID();?&gt;“ &lt;?php post_class(\'post\'); ?&GT; itemscope =“”itemtype =“http://schema.org/BlogPosting”&gt; &lt;?php
if(has_post_thumbnail()){
echo\'&lt; div class =“post-thumbnail” &gt;\';
echo\'&lt; a href =“\'。esc_url(get_permalink())。\'”&gt;\';
echo get_the_post_thumbnail($ post-&gt; ID,\'shop_isle_blog_image_size\');
echo\' &lt; / a&gt;\';
echo\'&lt; / div&gt;\';
}
?&gt;
&lt; div class =“tile_title”&gt;
&lt; a class =“post-title” &gt;&lt; a href =“&lt;?php echo esc_url(get_permalink());?&gt;”&gt;&lt;?php the_title(); ?&gt;&lt; / a&gt;&lt; / a&gt;
&lt; / div&gt;
&lt; / div&gt;
</ code> </ pre>
此外,缩略图应该会出现 来自与当前帖子相同的类别。 例如。 如果有人正在阅读“旅行”类别中的博客帖子,则4个缩略图应仅来自此类别,其他类别也是如此。 是否有可能做到这一点? </ p>
</ div>
网友观点:
This is called \\\"related posts\\\" feature. There are many plugins for this, for example YARPP is one such plugin.
If you want to avoid plugin, you can get theme that has this functionallity in the template itself. My blog for example uses Ribbon theme and contains this code.
Demand feedback