Press "Enter" to skip to content

添加随机文章小工具

在主题的functions文件里面添加如下代码段。

/**

  • Random_Posts widget class
    *
  • Author: haoxian_zeng https://cnzhx.net/
  • Date: 2013.05.14
    */
    //————— * 注册该微件
    class WP_Widget_myRandom_Posts extends WP_Widget { function construct() { $widget_ops = array(‘classname’ => ‘widget_my_random_posts’, ‘description’ => ( ‘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 = 10; // 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’]) : 10;
    ?>
    <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”);’ ) );

以上有个问题,就是文章数目无法正确设置,始终显示5篇,修改内部数字也是无效,蛮怪。相关引用信息已保留。

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注