|
一.随机文章的获取原理
WordPress 的文章查询函数 get_posts() 有个参数 orderby 指明了获取文章时的排序方式。通常我们是按照文章发布日期排序,比如 WordPress 站点首页、分类页和标签页中的文章列表。orderby 还可以是 rand,它使用 MySQL 的 RAND() 函数来确定排序参数,也就是随机排序。
使用方法如下:
- $args = array( 'numberposts' => 5, 'orderby' => 'rand', 'post_status' => 'publish' );
- $rand_posts = get_posts( $args );
复制代码
这样就能得到随机排序(第 2 个参数)的已发布(第 3 个参数)文章中的 5 篇(第 1 个参数)。
需要特别注意的是,如果没有第 3 个参数,它可能会将草稿之类的文章也显示出来。
二.向主题模板中添加
最直接的用法就是修改主题模板,在需要的位置放入下面的代码。
- <ul>
- <?php
- $args = array( 'numberposts' => 5, 'orderby' => 'rand', 'post_status' => 'publish' );
- $rand_posts = get_posts( $args );
- foreach( $rand_posts as $post ) : ?>
- <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
- <?php endforeach; ?>
- </ul>
复制代码
这样就能以列表的形式展示 5 篇带链接的随机文章。
优点:直接,可定制性强(可随意修改展示位置),消耗资源少
缺点:每次升级或更换主题都需要重新修改模板(使用子主题的话可能不需要每次升级都修改)
三.修改主题 functions.php 文件
注意:只适用于 PHP 5.2+
我的需求就是在 WordPress 后台中的“小工具”里增加一个“随机文章”的可用小工具,让我可以将其添加到边栏中作为微件显示出来。
现在我的 WordPress 中插件已经很多了,不希望增加太多的插件造成更多负担,所以就在主题的 functions.php 文件中增加了一个函数类来完成这个任务。
将下面的代码直接,或定制化修改后放入主题的 functions.php 文件中即可。如果当前使用的主题没有 functions.php 文件,可以在该主题的目录下自己创建一个,不过一定要注意在文档的开头输入 <?php,结尾输入 ?>。
成功后在 WordPress 后台的“小工具”页面可以找到对应的可用小工具(左图),添加到边栏后可设置微件标题和显示文章数目(右图)。页面显示效果如本页右上。
代码( // 后为注释):
- /**
- * Random_Posts widget class
- *
- * Author: haoxian_zeng <http://cnzhx.net/>
- * Date: 2013.05.14, cnzhx2011 1.0
- */
- //--------------- * 注册该微件
- class WP_Widget_myRandom_Posts extends WP_Widget {
- function __construct() {
- $widget_ops = array('classname' => 'widget_my_random_posts', 'description' => __( '水景一页定制的随机文章微件。The cnzhx customized random posts widget.' ) );
- parent::__construct('random-posts', __('随机文章 Random Posts'), $widget_ops);
- $this->alt_option_name = 'widget_my_random_posts';
- }
- function widget( $args, $instance ) {
- global $randomposts, $post;
- extract($args, EXTR_SKIP);
- $output = '';
- // 设置 widget 标题
- $title = apply_filters('widget_title', empty($instance['title']) ? __('随机文章 Random Posts') : $instance['title']);
- // 设置要获取的文章数目
- if ( ! $number = absint( $instance['number'] ) )
- $number = 5;
- // WP 数据库查询,使用 rand 参数来获取随机的排序,并取用前面的 $number 个文章
- $randomposts = get_posts( array( 'number' => $number, 'orderby' => 'rand', 'post_status' => 'publish' ) );
- // 下面开始准备输出数据
- // 先输出一般的 widget 前缀
- $output .= $before_widget;
- // 输出标题
- if ( $title )
- $output .= $before_title . $title . $after_title;
- // random posts 列表开始
- $output .= '<ul id="randomposts">';
- if ( $randomposts ) {
- foreach ( (array) $randomposts as $post) {
- $output .= '<li><a href="' . get_permalink() . '">' . $post->post_title . '</a></li>';
- }
- }
- $output .= '</ul>';
- // 输出一般的 widget 后缀
- $output .= $after_widget;
- // 输出到页面
- echo $output;
- }
- function update( $new_instance, $old_instance ) {
- $instance = $old_instance;
- $instance['title'] = strip_tags($new_instance['title']);
- $instance['number'] = absint( $new_instance['number'] );
- $alloptions = wp_cache_get( 'alloptions', 'options' );
- if ( isset($alloptions['widget_my_random_posts']) )
- delete_option('widget_my_random_posts');
- return $instance;
- }
- //
- // 在 WP 后台的 widget 内部显示两个参数, 1. 标题;2. 显示文章数目
- //
- function form( $instance ) {
- $title = isset($instance['title']) ? esc_attr($instance['title']) : '';
- $number = isset($instance['number']) ? absint($instance['number']) : 5;
- ?>
- <p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?></label>
- <input class="cnzhx" 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
- }
- }
- // register WP_Widget_myRandom_Posts widget
- add_action( 'widgets_init', create_function( '', 'return register_widget("WP_Widget_myRandom_Posts");' ) );
复制代码
优点:比方法 1 费资源、比方法 2 省资源,可定制化程度极高
缺点:麻烦
|
|