搜索
查看: 858|回复: 0

知更鸟begin在文章后面增加相关文章

[复制链接]
发表于 2018-11-20 16:20:16 | 显示全部楼层 |阅读模式
最近想到在文章后面手动加一个相关文章,增加网站的粘性

首先找到social.php文件
路径:/wp-content/themes/begin/template/social.php

在最顶端加入以下代码:
  1. <!-- 相关文章开始 -->
  2. <ul class="spostinfo">
  3. <h3>80%的人都看过的文章</h3>
  4. <ul class="related_posts">
  5. <?php
  6. $post_num = 8;
  7. $exclude_id = $post->ID;
  8. $posttags = get_the_tags(); $i = 0;
  9. if ( $posttags ) {
  10.         $tags = ''; foreach ( $posttags as $tag ) $tags .= $tag->term_id . ',';
  11.         $args = array(
  12.                 'post_status' => 'publish',
  13.                 'tag__in' => explode(',', $tags),
  14.                 'post__not_in' => explode(',', $exclude_id),
  15.                 'caller_get_posts' => 1,
  16.                 'orderby' => 'comment_date',
  17.                 'posts_per_page' => $post_num,
  18.         );
  19.         query_posts($args);
  20.         while( have_posts() ) { the_post(); ?>
  21.                 <li><a rel="bookmark" href="<?php the_permalink(); ?>" title="<?php the_title(); ?>" target="_blank"><?php the_title(); ?></a></li>
  22.         <?php
  23.                 $exclude_id .= ',' . $post->ID; $i ++;
  24.         } wp_reset_query();
  25. }
  26. if ( $i < $post_num ) {
  27.         $cats = ''; foreach ( get_the_category() as $cat ) $cats .= $cat->cat_ID . ',';
  28.         $args = array(
  29.                 'category__in' => explode(',', $cats),
  30.                 'post__not_in' => explode(',', $exclude_id),
  31.                 'caller_get_posts' => 1,
  32.                 'orderby' => 'comment_date',
  33.                 'posts_per_page' => $post_num - $i
  34.         );
  35.         query_posts($args);
  36.         while( have_posts() ) { the_post(); ?>
  37.                 <li><a rel="bookmark" href="<?php the_permalink(); ?>"  title="<?php the_title(); ?>" target="_blank"><?php the_title(); ?></a></li>
  38.         <?php $i++;
  39.         } wp_reset_query();
  40. }
  41. if ( $i  == 0 )  echo '<li>没有相关文章!</li>';
  42. ?>
  43. <!-- 相关文章结束 -->
复制代码


您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

麦屯网

网站简介:专业发烧音乐试听,最新专辑,发烧器材评测,音乐影视信息交流。

联系我们

  • 反馈邮箱:sorry16817#gmail.com

麦屯博客 |网站地图

Powered by maitun.net

快速回复 返回顶部 返回列表