SEO建站添加相关文章功能(WordPress标题列表样式)

我们在SEO建站的过程中,需要根据网站数据对网站结构做适当的调整,就拿MAY的SEO博客来说吧,网站的跳出率和页面访问深度一直不尽人意,所以May想给文章的底部比较显眼之处新增相关文章的功能。当然WordPress相关文章功能的插件很多,但是为了这么个小功能就会使用插件有点大材小用了,其实我们可以通过代码来轻松实现。

SEO建站添加相关文章功能(WordPress标题列表样式)文章源自MAY的SEO博客-https://may90.com/building/related.html

SEO建站: Step 1. 复制下面的代码至想要显示相关文章即可

如果您是想放在文章下方,建议将代码添加到single.php中;如果您想放在文章内容下方,就同May一样,就将代码放在content.php中。文章源自MAY的SEO博客-https://may90.com/building/related.html

代码中$post_num = 8; 其中的8可以修改为您想展示的文章数目。文章源自MAY的SEO博客-https://may90.com/building/related.html

<h3>相关文章</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>';
?>
</ul>

SEO建站: Step 2. 添加CSS样式

以下的CSS样式效果,供大家参考使用。文章源自MAY的SEO博客-https://may90.com/building/related.html

/*文正内容模块新增“您可能感兴趣的文章”*/
.may-add h3{font-size: 1.6rem;}
.related_posts{margin-top:5px;}
.related_posts li{margin-left:20px;font-size:100%;line-height:1.7;padding:0 0 0 5px; list-style: disc;}
.related_posts a{color:#0f92fb;}
.related_posts a:hover{color:#444;}

SEO建站:Step 3. 查看效果

SEO建站:相关文章效果展示文章源自MAY的SEO博客-https://may90.com/building/related.html

以上,就是SEO建站添加相关文章功能(WordPress标题列表样式)的操作。文章源自MAY的SEO博客-https://may90.com/building/related.html 文章源自MAY的SEO博客-https://may90.com/building/related.html

  • 我的微信
  • 扫一扫微信,添加好友
  • weinxin
  • 我的微信公众号
  • 关注公众号,了解最新动态
  • weinxin
  • 本文由 发表于 2019年5月24日
  • 转载请务必保留本文链接:https://may90.com/building/related.html
评论  4  访客  2  作者  2
    • 张波博客 2

      你的这个站现在被你美化的看着很舒服!

        • MAY的SEO博客

          @ 张波博客 哈哈,谢谢。多来看看 :razz:

        • 道德经 0

          WP真是很强大啊

            • MAY的SEO博客

              @ 道德经 是的,网上也有各种WP的教程及资源,轻松易上手。

          发表评论

          匿名网友 填写信息

          :?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

          确定