使用隨機文章的好處:

避免好的文章被淹沒了,搜尋引擎每次來訪問的是不同的連結,增加內鏈。可以自由拖放,與其他小工具搭配使用,避免寫在側邊欄過於死板。

為 WP 主題新增隨機文章小工具方法:

首先找到主題的 functions.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>
<?php while ($r->have_posts()) : $r->the_post(); ?>
<li style="border-top: 1px solid rgba(255, 255, 255, 0.2); padding: 8px 0 9px;border-color: rgba(0, 0, 0, 0.1);"><a href="<?php%20the_permalink()%20?>" 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");'));

什麼?看不懂?沒關係,不需要看懂,實在怕搞錯的,不想動手的童鞋可以試一試外掛的方法,比如:中文工具箱、 Random Pages Widget 等等。