糀絮錵落 发表于 2018-11-20 16:20:16

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

最近想到在文章后面手动加一个相关文章,增加网站的粘性

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

在最顶端加入以下代码:
<!-- 相关文章开始 -->
<ul class="spostinfo">
<h3>80%的人都看过的文章</h3>
<ul class="related_posts">
<?php
$post_num = 8;
$exclude_id = $post->ID;
$posttags = get_the_tags(); $i = 0;
if ( $posttags ) {
        $tags = ''; foreach ( $posttags as $tag ) $tags .= $tag->term_id . ',';
        $args = array(
                'post_status' => 'publish',
                'tag__in' => explode(',', $tags),
                'post__not_in' => explode(',', $exclude_id),
                'caller_get_posts' => 1,
                'orderby' => 'comment_date',
                'posts_per_page' => $post_num,
        );
        query_posts($args);
        while( have_posts() ) { the_post(); ?>
                <li><a rel="bookmark" href="<?php the_permalink(); ?>" title="<?php the_title(); ?>" target="_blank"><?php the_title(); ?></a></li>
        <?php
                $exclude_id .= ',' . $post->ID; $i ++;
        } wp_reset_query();
}
if ( $i < $post_num ) {
        $cats = ''; foreach ( get_the_category() as $cat ) $cats .= $cat->cat_ID . ',';
        $args = array(
                'category__in' => explode(',', $cats),
                'post__not_in' => explode(',', $exclude_id),
                'caller_get_posts' => 1,
                'orderby' => 'comment_date',
                'posts_per_page' => $post_num - $i
        );
        query_posts($args);
        while( have_posts() ) { the_post(); ?>
                <li><a rel="bookmark" href="<?php the_permalink(); ?>"title="<?php the_title(); ?>" target="_blank"><?php the_title(); ?></a></li>
        <?php $i++;
        } wp_reset_query();
}
if ( $i== 0 )echo '<li>没有相关文章!</li>';
?>
<!-- 相关文章结束 -->

页: [1]
查看完整版本: 知更鸟begin在文章后面增加相关文章