继续折腾博客,想在文章后面加个带图片的同分类随机文章。百度了下,方法如下:
一、复制下面代码粘贴到主题functions.php中。
- //支持外链缩略图
 - if ( function_exists('add_theme_support') )
 - add_theme_support('post-thumbnails');
 - function catch_first_image()
 - {
 - global $post, $posts;$first_img = '';
 - ob_start();
 - ob_end_clean();
 - $output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
 - $first_img = $matches [1] [0];
 - //如果第一张图不存在,则返回随机图片
 - if(emptyempty($first_img)){
 - $first_img = '';
 - //从2张图中随机选择,可根据自己的图片数量设置
 - $random = mt_rand(1, 85);
 - echo get_bloginfo ( 'stylesheet_directory' );
 - echo '/images/random/'.$random.'.jpg';
 - }
 - return $first_img;
 - }
 
二、加入代码,可以调用到文章页面,也可以直接加入single.php里。
- <?php
 - if ( is_single() ) :
 - global $post;
 - $categories = get_the_category();
 - foreach ($categories as $category) :
 - ?>
 - <div class="related">
 - <div class="titlecat"><i class="zm zm-fenlei" style="margin-right: 1px;font-size: 1rem!important;"></i><span class="re-title">同类文章</span>
 - </div>
 - <div id="related-img">
 - <?php
 - $posts = get_posts('numberposts=4&orderby=rand&category='. $category->term_id);
 - foreach($posts as $post) :
 - ?>
 - <div class="r4">
 - <div class="related-site">
 - <figure class="related-site-img">
 - <span class="">
 - <a class="sc" href="<?php the_permalink() ?>" target="_blank" rel="nofollow" title="<?php printf(__('%s', 'kubrick'), the_title_attribute('echo=0')); ?>"><img class="post-img" width="280" height="210" src="<?php echo catch_first_image(); ?>" alt="<?php printf(__('%s', 'kubrick'), the_title_attribute('echo=0')); ?>" title="<?php printf(__('%s', 'kubrick'), the_title_attribute('echo=0')); ?>" itemprop="image" loading="lazy">
 - </a>
 - </span>
 - </figure>
 - <div class="related-title">
 - <div class="retitle">
 - <span class="zm zm-lanmulvshisuibi" style="color: #333;"></span><a class="arelated" target="_blank" href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a>
 - </div>
 - </div>
 - </div>
 - </div>
 - <?php endforeach; ?>
 - </div>
 - </div>
 - <?php
 - endforeach; endif ; ?>
 - <?php
 - ?>
 
以上是源代码。
加入到文章里面即可,但是发现添加以上代码后,文章的评论会显示的是随机最后一篇文章的评论,这就头疼了,对于菜鸟级别的我就懵逼了。但是又发现其实评论是显示评论上面文章id的评论,那么我把同类文章的代码加在了评论后,这样就顺利显示了文章的评论了。
三、把评论和同类文章框在一个层里,这样可以通过display的column-reverse来控制显示顺序,反顺序显示就相当于同类文章在前,评论在后了。
- <div class="com-re" style="display: flex;flex-direction: column-reverse;">
 - <div class="com-t"><?php comments_template(); ?></div>
 - <div>
 - <?php include('related-posts.php');?>
 - <hr class="hr-twill-colorful" data-content="◆" style="width: 99%;display: block!important;">
 - </div>
 - </div>
 
四、再加入以下css美化
- .related {
 - margin-top: 20px;
 - }
 - .titlecat {
 - margin-bottom: 5px;
 - color: #333;
 - font-size: 1rem;
 - margin-left: 10px;
 - }
 - #related-img {
 - display: flex;
 - background: #fff;
 - margin-bottom: 0px;
 - padding: 0px 5px;
 - border: 0px solid #e7e7e7;
 - flex-wrap: wrap;
 - justify-content: space-between;
 - }
 - .related-site {
 - max-width: 100%;
 - width: 100%;
 - height: auto;
 - padding: 5px;
 - }
 - .related-site-img {
 - position: relative;
 - max-width: 100%;
 - width: 100%;
 - height: auto;
 - overflow: hidden;
 - }
 - .related-site-img a img {
 - float: left;
 - max-width: 100%;
 - width: 100%;
 - height: 126px;
 - margin: 0;
 - border-radius: 3px 3px 0 0;
 - padding: 0;
 - }
 - .related-site-img a img:hover {
 - float: left;
 - max-width: 100%;
 - width: 100%;
 - border-radius: 3px 3px 0 0;
 - height: 126px;
 - margin: 0;
 - padding: 0;
 - }
 - .related-title {
 - padding: 5px;
 - background: #dee3ea;
 - height: 48px;
 - border-radius: 0 0 3px 3px;
 - overflow: hidden;
 - margin: 0;
 - -webkit-transition: all .4s ease-in-out;
 - -moz-transition: all .4s ease-in-out;
 - -ms-transition: all .4s ease-in-out;
 - -o-transition: all .4s ease-in-out;
 - display: flex;
 - align-items: center;
 - }
 - .related-title:hover {
 - padding: 5px;
 - background: #e8eef6;
 - height: 48px;
 - border-radius: 0 0 3px 3px;
 - overflow: hidden;
 - margin: 0;
 - -webkit-transition: all .4s ease-in-out;
 - -moz-transition: all .4s ease-in-out;
 - -ms-transition: all .4s ease-in-out;
 - -o-transition: all .4s ease-in-out;
 - display: flex;
 - align-items: center;
 - }
 - .arelated {
 - color: #333;
 - }
 - .arelated:hover {
 - color: #333;
 - }
 
完工,但是由于是通过 get_the_category()来获取的,如果文章存在属于多个分类的话,会出现多个同分类,所以建议出现此情况的文章删除多个分类即可。目前还没有找到评论问题的最佳解决办法,如果哪位大佬看到,希望指点下龙哥,谢谢!
真开心,有大佬留言帮解决了,非常感谢!办法如下:
把foreach($posts as $post) 改成 foreach($posts as $post_tmp),当然修改后得把评论和同分类的源码替换下,然后删除
- style="display: flex;flex-direction: column-reverse;"
 
但是问题又出现了,虽然解决了前面问题,但是会出现新的问题就是:如果当前文章有图片的话,那么同分类随即文章图片以及文章信息都会直接显示当前的文章的缩影图以及文章链接。
弄这么复杂干啥?就直接在文章模板页面直接写查询数据的代码,应该就好了。我的站点多数功能都是我直接查询数据库增加的功能。
foreach($posts as $post) 改成 foreach($posts as $post_tmp) 试试。
非常感谢!终于解决了。你的网站打不开哦。
好像出问题了,就是如果文章没有图片的话,正常显示,但是如果当前文章有图片,那么同分类随即文章(四个都会)会只显示当前的文章和缩影图。