当前位置:首页文章笔记建站教程无需插件快速实现WordPress相关文章调用方法

无需插件快速实现WordPress相关文章调用方法

我们很多网友会发现从 WordPress 官方平台下载的很多国外的主题没有页面相关文章调用列表,这个相对于 SEO 来说可能会有部分影响,至少让用户看不到同类的文章。那就需要我们自己添加这些功能,当然包括有一些插件可以实现,比如【Related Posts Thumbnails Plugin for WordPress 】插件是可以实现的,但是我们不要这么复杂的插件,这里我们就整理简单的代码实现。

  1. <div class="related_posts">
  2. <h3>这几篇文章你可能也喜欢:</h3>
  3. <ul>
  4. <?php
  5. $post_num = 10;
  6. $exclude_id = $post->ID;
  7. $posttags = get_the_tags(); $i = 0;
  8. if ( $posttags ) {
  9. $tags = ; foreach ( $posttags as $tag ) $tags .= $tag->term_id . ‘,’;
  10. $args = array(
  11. ‘post_status’ => ‘publish’,
  12. ‘tag__in’ => explode(‘,’, $tags),
  13. ‘post__not_in’ => explode(‘,’, $exclude_id),
  14. ‘caller_get_posts’ => 1,
  15. ‘orderby’ => ‘comment_date’,
  16. ‘posts_per_page’ => $post_num,
  17. );
  18. query_posts($args);
  19. while( have_posts() ) { the_post(); ?>
  20. <li><a rel="bookmark" href="<?php the_permalink(); ?>" title="<?php the_title(); ?>" target="_blank"><?php the_title(); ?></a></li>
  21. <?php
  22. $exclude_id .= ‘,’ . $post->ID; $i ++;
  23. } wp_reset_query();
  24. }
  25. if ( $i < $post_num ) {
  26. $cats = ; foreach ( get_the_category() as $cat ) $cats .= $cat->cat_ID . ‘,’;
  27. $args = array(
  28. ‘category__in’ => explode(‘,’, $cats),
  29. ‘post__not_in’ => explode(‘,’, $exclude_id),
  30. ‘caller_get_posts’ => 1,
  31. ‘orderby’ => ‘comment_date’,
  32. ‘posts_per_page’ => $post_num $i
  33. );
  34. query_posts($args);
  35. while( have_posts() ) { the_post(); ?>
  36. <li><a rel="bookmark" href="<?php the_permalink(); ?>" title="<?php the_title(); ?>" target="_blank"><?php the_title(); ?></a></li>
  37. <?php $i++;
  38. } wp_reset_query();
  39. }
  40. if ( $i == 0 ) echo ‘<li>没有相关文章!</li>’;
  41. ?>
  42. </ul>
  43. </div>

我们将代码添加到对应需要展示单页的页面位置。然后还需要自定义样式。

  1. .related_posts {
  2. margin-top: 5px;
  3. padding-bottom: 10px;
  4. border-bottom: 1px solid #ededed;
  5. }
  6. .related_posts h3 {
  7. margin-bottom: 5px;
  8. }
  9. .related_posts li {
  10. margin-left: 20px;
  11. color: #ccc;
  12. list-style: square;
  13. font-size: 14px;
  14. line-height: 26px;
  15. padding: 0 0 0 5px
  16. }
温馨提示:

文章标题:无需插件快速实现WordPress相关文章调用方法

文章链接:https://www.wuyanshuo.cn/780.html

更新时间:2022年04月23日

本站大部分内容均收集于网络!若内容若侵犯到您的权益,请发送邮件至:service@wuyanshuo.cn我们将第一时间处理! 资源所需价格并非资源售卖价格,是收集、整理、编辑详情以及本站运营的适当补贴,并且本站不提供任何免费技术支持。 所有资源仅限于参考和学习,版权归原作者所有,更多请阅读无言说网络服务协议

给TA打赏
共{{data.count}}人
人已打赏
建站教程

WordPress 禁止用户删除某些文章类型中的文章

2022-4-22 11:13:49

建站教程

更换WordPress默认分页/作者/搜索目录标签名称

2022-4-23 17:00:21

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索
'