麦屯网

标题: 知更鸟begin在文章后面增加相关文章 [打印本页]

作者: 糀絮錵落    时间: 2018-11-20 16:20
标题: 知更鸟begin在文章后面增加相关文章
最近想到在文章后面手动加一个相关文章,增加网站的粘性

首先找到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. <!-- 相关文章结束 -->
复制代码







欢迎光临 麦屯网 (https://www.maitun.net/) Powered by Discuz! X3.4