WordPress添加随机文章小工具(随机文章对网站SEO的作用)

- 作者:小小课堂网 - 阅 2,220   推荐

WordPress添加随机文章小工具,一般WordPress博客默认小工具可以添加近期文章列表,可近期文章本来就在首页有大面积展示,在右侧边栏显示略显浪费首页位置,故灬无言建议右侧添加随机文章列表。今天,小小课堂网( xxkt.org.cn )为大家介绍《WordPress添加随机文章小工具(随机文章对网站SEO的作用)》。希望对大家有所帮助。

随机文章的作用是什么

一、随机文章的作用

1、充分收录文章

网站首页的随机文章可以让搜索引擎将网站文章收录的更彻底,因为一般情况下搜索引擎蜘蛛光临首页的次数是最多的。这样蜘蛛可以去爬行那些被随机展示出来的且未被收录的文章,让网站文章收录更充分。如何让百度快速收录,参考以往教程《如何让百度快速收录网站及文章?_小小课堂网》。

2、提升快照更新频率

如果我们的网站在一段时间内不更新,也就是说首页几个月都是同样的,没有一丝变化,相信快照更新的时间会很长。可如果我们增加了随机文章列表,让首页保持着变化,会缩短快照更新的时间。什么是百度快照,参考以往教程《什么是百度快照?对SEO优化有用吗?》。

3、用户的驻留时间增长

用户在看到随机文章列表时,很有可能以往的文章更能吸引他的目光,从而完成一次新的点击,PV的提升,驻留时间自然会增长。

二、WordPress添加随机文章小工具

可以由插件实现,也可以通过后台添加代码来实现。这里小小课堂网仅介绍代码实现,因为解决问题的方式更直接。

在主题目录下找到并修改functions.php文件。

一般在wp-content > themes文件夹下,打开之后,在结尾处添加如下代码。

 //随机文章
class RandomPostWidget extends WP_Widget
{
 function RandomPostWidget()
 {
 parent::WP_Widget('bd_random_post_widget', '随机文章', array('description' => '我的随机文章小工具') );
 }
 function widget($args, $instance)
 {
 extract( $args );
 $title = apply_filters('widget_title',empty($instance['title']) ? '随机文章' :
 $instance['title'], $instance, $this->id_base);
 if ( empty( $instance['number'] ) || ! $number = absint( $instance['number'] ) )
 {
 $number = 10;
 }
 $r = new WP_Query(array('posts_per_page' => $number, 'no_found_rows' => true,'post_status' => 'publish', 'ignore_sticky_posts' => true, 'orderby' =>'rand'));
 if ($r->have_posts())
 {
 echo $before_widget;
 if ( $title ) echo $before_title . $title . $after_title;
?>
 <ul>
 <?php while ($r->have_posts()) : $r->the_post(); ?>
 <li>
 <a href="<?php the_permalink() ?>" title="<?php echo esc_attr(get_the_title() ? get_the_title() : get_the_ID()); ?>"><?php if ( get_the_title() ) the_title(); else the_ID(); ?></a>
 </li>
 <?php endwhile; ?>
 </ul>
 <?php
 echo $after_widget;
 wp_reset_postdata();
 }
 }
function update($new_instance, $old_instance)
{
 $instance = $old_instance;
 $instance['title'] = strip_tags($new_instance['title']);
 $instance['number'] = (int) $new_instance['number'];
 return $instance;
}
function form($instance)
{
 $title = isset($instance['title']) ? esc_attr($instance['title']) : '';
 $number = isset($instance['number']) ? absint($instance['number']) : 10;?>
 <p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?></label>
 <input id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" /></p>
 
 <p><label for="<?php echo $this->get_field_id('number'); ?>"><?php _e('文章显示数量:'); ?></label>
 <input id="<?php echo $this->get_field_id('number'); ?>" name="<?php echo $this->get_field_name('number'); ?>" type="text" value="<?php echo $number; ?>" size="3" /></p>
<?php
}
}
add_action('widgets_init', create_function('', 'return register_widget("RandomPostWidget");'));

在小工具这里就可以看到我们新添加的“随机文章”啦。

小工具随机文章

以上就是小小课堂网( xxkt.org.cn )为大家介绍《WordPress添加随机文章小工具(随机文章对网站SEO的作用)》。感谢您的阅读。百度SEO培训认准小小课堂!

本文最后一次更新时间:2022年2月28日

本文《WordPress添加随机文章小工具(随机文章对网站SEO的作用)》由小小课堂网整理或原创,侵删,欢迎转载并保留版权:https://xxkt.org.cn/ 感谢您的阅读。

本站提供SEO培训、咨询、诊断,QQ:2216876660 微信:huowuyan 公众号:小小课堂网

标签:

相关文章!
error: Content is protected !!