增加网站之间的内链有利于SEO。可以通过在文章末端增加相关文章推荐、热评文章推荐、最新文章推荐来提高网站的内链程度。
示例
添加之后的效果
代码详解
针对handsome主题,直接放在主题文件夹下的post.php
文件里。具体位置如下图所示。
相关文章推荐
这里的推荐是根据标签来推荐的。相同的标签则认为是相关的文章,因此在写文章的时候要注意标签的建设。
<div class="tab-content clear">
<h4 class="widget-title m-t-none text-md"><?php _me("※相关文章推荐※ ") ?>
</h4>
<div id="relatedPosts" >
<ul class="list-group-item nav nav-list">
<?php $this->related(6)->to($relatedPosts); ?>
<?php if ($relatedPosts->have()): ?>
<?php while ($relatedPosts->next()): ?>
<li >
<a href="<?php $relatedPosts->permalink(); ?>" title="<?php $relatedPosts->title(); ?>"><?php $relatedPosts->title(); ?></a>
</li>
<?php endwhile; ?>
<?php else : ?>
<li>暂无相关推荐</li>
<?php endif; ?>
</ul>
</div>
</div>
数字6即最多显示6篇。不建议显示太多。
热门文章推荐
首先,在function.php的底部增加一个函数:
function getHotComments($limit = 5){
$db = Typecho_Db::get();
$result = $db->fetchAll($db->select()->from('table.contents')
->where('status = ?','publish')
->where('type = ?', 'post')
->where('created <= unix_timestamp(now())', 'post')
->limit($limit)
->order('commentsNum', Typecho_Db::SORT_DESC)
);
if($result){
foreach($result as $val){
$val = Typecho_Widget::widget('Widget_Abstract_Contents')->push($val);
$post_title = htmlspecialchars($val['title']);
$permalink = $val['permalink'];
echo '<li><a href="'.$permalink.'" title="'.$post_title.'" target="_blank">'.$post_title.'</a></li>';
}
}
}
随后在post.php
文件里增加代码:
<div class="tab-content clear">
<h4 class="widget-title m-t-none text-md"><?php _me("※热评文章推荐※") ?>
</h4>
<div id="relatedPosts" >
<ul class="list-group-item nav nav-list">
<li> <?php getHotComments('5');?> </li>
</ul>
</div>
</div>
<br >
数字5就是最多显示5篇。
最新文章
<div class="tab-content clear">
<h4 class="widget-title m-t-none text-md"><?php _me("※最新文章推荐※") ?>
</h4>
<div id="relatedPosts" >
<ul class="list-group-item nav nav-list">
<?php
$recent = $this->widget('Widget_Contents_Post_Recent','pageSize=6');
if($recent->have()):
while($recent->next()):
?>
<li><a href="<?php $recent->permalink();?>"><?php $recent->title();?></a></li>
<?php endwhile; endif;?>
</ul>
</div>
</div>
pageSize=6
指最多显示6篇。
数量上可以自行设置参数。样式可以自定义CSS。我觉得没必要做的太花哨。