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.  
  17. }

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

已有 202 人购买
查看演示升级 VIP立刻购买

收藏
(0)

发表回复

热销模板

Ashade - 作品展示摄影相册WordPress汉化主题
LensNews

本站承接 WordPress / PbootCMS / DedeCMS 等
系统建站、仿站、开发、定制等业务!