大家网站的底部一般需要加上一个相关文章列表,可以是图片的也可以是文字的。比如我这个加的就是图片列表:
一方面算是网站的内链,利于百度SEO蜘蛛抓取文章?
我也不知道是不是真的。
一方面可以更好地留存访客,增加黏性。
访客万一看到感兴趣的,就会也点开看下。
如何添加呢?这个功能很多插件并不能很好的实现,以前的一堆社会化的代码 也挂了。
下面我来给大家示范怎么写这段代码:
需要修改的文件为 wp-content目录下的 themes 目录下的相关主题文档的 page.php 与single.php的 文章底部位置。
调用同标签的文章一定数量(数量需要您设置)如果同标签没有这么多篇文章,那就调用同分类的文章。
下面的是我先写好的代码(本站目前在用的)
<div class="row"> <!--下面这段代码将会被程序循环--> <div class="col-md-4 col-xs-6"> <div class="thumbnail"> <a href="#"> <img class="suoluetu" src="图片链接" alt="图片标题" /> </a> <div class="caption"> <a href="#">文章标题</a> </div> </div> </div> <!--上面这段代码将会被程序循环--> </div>
本段代码没有添加循环代码的html原始文件(主要用于大家对比参考)
添加好循环代码后的文件是啥样的呢?
<div class="row"> <?php $post_num = 6; // 默认展示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(); ?> <div class="col-md-4 col-xs-6"> <div class="thumbnail"> <a href="<?php the_permalink(); ?>"> <?php if ( $values = get_post_custom_values("suoluetu") ) { ?> <img class="suoluetu" src="<?php $values = get_post_custom_values("suoluetu"); echo $values[0]; ?>" alt="<?php the_title(); ?>" /> <?php } else { ?> <img class="suoluetu" src="https://img.wpyi.com/images/suoluetu.gif" alt="<?php the_title(); ?>" /> <?php } ?> </a> <div class="caption"> <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a> </div> </div> </div> <?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(); ?> <div class="col-md-4 col-xs-6"> <div class="thumbnail"> <a href="<?php the_permalink(); ?>"> <?php if ( $values = get_post_custom_values("suoluetu") ) { ?> <img class="suoluetu" src="<?php $values = get_post_custom_values("suoluetu"); echo $values[0]; ?>" alt="<?php the_title(); ?>" /> <?php } else { ?> <img class="suoluetu" src="https://img.wpyi.com/images/suoluetu.gif" alt="<?php the_title(); ?>" /> <?php } ?> </a> <div class="caption"> <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a> </div> </div> </div> <?php $i++; } wp_reset_query(); } if ( $i == 0 ) echo '<div class="col-md-3 col-xs-6">没有相关文章!</div>'; ?> </div>
本段代码的作用是 调用同标签的文章一定数量(数量需要您设置)如果同标签没有这么多篇文章,那就调用同分类的文章。
上面那段代码为图文列表,如果只是显示可能感兴趣的文章列表的话,代码为:
<ul> <?php $post_num = 9; // 默认展示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 href="<?php the_permalink(); ?>"><?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 href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li> <?php $i++; } wp_reset_query(); } if ( $i == 0 ) echo '<li>没有相关文章!</li>'; ?> <div class="clear"></div> </ul>
本方法是获取该文章的分类 id,然后获取该分类下的文章,来达到获取相关文章的目的。
<ul id="cat_related"> <?php $cats = wp_get_post_categories($post->ID); if ($cats) { $cat = get_category( $cats[0] ); $first_cat = $cat->cat_ID; $args = array( 'category__in' => array($first_cat), 'post__not_in' => array($post->ID), 'showposts' => 6, 'caller_get_posts' => 1); query_posts($args); if (have_posts()) : while (have_posts()) : the_post(); update_post_caches($posts); ?> <li><a href="<?php the_permalink(); ?>" rel="bookmark" title="<?php the_title_attribute();?>"><?php the_title(); ?></a></li> <?php endwhile; else : ?> <li>暂无相关文章</li> <?php endif; wp_reset_query(); } ?> </ul>
获取为文章添加的所有标签,接着获取这些标签下的文章,那么这些文章就是与该文章相关的文章了。
<ul> <?php $post_tags = wp_get_post_tags($post->ID); if ($post_tags) { foreach ($post_tags as $tag) { // 获取标签列表 $tag_list[] .= $tag->term_id; } // 随机获取标签列表中的一个标签 $post_tag = $tag_list[ mt_rand(0, count($tag_list) - 1) ]; // 该方法使用 query_posts() 函数来调用相关文章,以下是参数列表 $args = array( 'tag__in' => array($post_tag), 'category__not_in' => array(NULL), // 不包括的分类ID 'post__not_in' => array($post->ID), 'showposts' => 6, // 显示相关文章数量 'caller_get_posts' => 1 ); query_posts($args); if (have_posts()) : while (have_posts()) : the_post(); update_post_caches($posts); ?> <li><a href="<?php the_permalink(); ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></li> <?php endwhile; else : ?> <li>暂无相关文章</li> <?php endif; wp_reset_query(); } ?> </ul>