博客的文章页面添加随机文章,不仅可以增强用户粘度,还可以把一些陈芝麻烂谷子的文章给时不时的翻出来,不至于沉下去。除了这下,还有一个更重要的作用,那就是当搜索引擎来爬你的文章的时候,每次都会有变化,同时还能增加文章内链。
关于如何在wordpress添加随机文章,时空在经过百度谷歌之后找到的大部分都是以下两种办法:一、使用插件;二、自己写代码。
关于插件,有很多很多,如中文工具箱、Random Pages Widget等等,对不想折腾代码的同学不失为不错的方法,简单省事,使用容易,最大的问题就是插件多了会影响一点点速度。可不可以实现自定义的小工具呢,那种可以在后台拖放使用的小工具,而不是死死的写在模板文件中。这样有很多好处,我们可以自由地拖放,想放哪就放哪,这样就可以和其他小工具和谐地相处了,不用去修改模板文件。时空在苦苦搜寻之后终于找到以下方法可以完美实现。
效果可以看我博客的左侧侧。
将以下代码添加到主题的function.php中,加到最后的?>之前或是开头:
//随机文章小工具
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 "n";
echo $before_widget;
if ( $title ) echo $before_title . $title . $after_title;
?>
<ul class="line">
<?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('Number of posts to
show:'); ?></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");'));
以下是代码注释:
//parent::直接使用父类中的方法
//$name 这个小工具的名称,
//$widget_ops 可以给小工具进行描述等等。
//$control_ops 可以对小工具进行简单的样式定义等等。
//__construct() 是构造函数,你也可以用 Radom_Posts() 代替以使程序能支持 PHP4。
//widget() 是小工具功能实现并且用于输出的方法,这里写上我们获得随机文章的代码,并且 echo 出用于前台显示。
//update() 是我们拖放小工具到边栏时设置的数据更新,比如我们的 WordPress 近期文章小工具,会要我们输入“标题”和“显示文章数”,我们输入后保存即可,这个函数就是用来更新数据的
//form() 这是用于在后台拖动到边栏中时显示的样式,我们可以定义“标题”和“显示文章数”等,这里我们根据需要而定,可以让小工具更灵活。
//mywidget_register_widgets() 函数是在这个 Radom_Posts 类之外的,mywidget_register_widgets() 函数中利用register_widget() 函数将 Radom_Posts 类注册成为小工具。
//最后就是用添加动作 add_action() 函数将mywidget_register_widgets() 函数加到WordPress 的 widgets_init 钩子中去。大功告成,如果你的主题支持小工具,你去后台小工具就会发现左边多了一个叫 随机文章 的小工具,可以拖放使用。