请选择 进入手机版 | 继续访问电脑版

 找回密码
 立即注册
查看: 1524|回复: 0

给 WordPress 博客添加随机文章的3个方法

[复制链接]

6771

主题

8

回帖

2万

积分

管理员

积分
21718
发表于 2021-3-14 22:25:41 | 显示全部楼层 |阅读模式
一.随机文章的获取原理

WordPress 的文章查询函数 get_posts() 有个参数 orderby 指明了获取文章时的排序方式。通常我们是按照文章发布日期排序,比如 WordPress 站点首页、分类页和标签页中的文章列表。orderby 还可以是 rand,它使用 MySQL 的 RAND() 函数来确定排序参数,也就是随机排序。

使用方法如下:

  1. $args = array( 'numberposts' => 5, 'orderby' => 'rand', 'post_status' => 'publish' );
  2. $rand_posts = get_posts( $args );
复制代码


这样就能得到随机排序(第 2 个参数)的已发布(第 3 个参数)文章中的 5 篇(第 1 个参数)。

需要特别注意的是,如果没有第 3 个参数,它可能会将草稿之类的文章也显示出来。



二.向主题模板中添加

最直接的用法就是修改主题模板,在需要的位置放入下面的代码。

  1. <ul>
  2. <?php
  3. $args = array( 'numberposts' => 5, 'orderby' => 'rand', 'post_status' => 'publish' );
  4. $rand_posts = get_posts( $args );
  5. foreach( $rand_posts as $post ) : ?>
  6.     <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
  7. <?php endforeach; ?>
  8. </ul>
复制代码



这样就能以列表的形式展示 5 篇带链接的随机文章。

优点:直接,可定制性强(可随意修改展示位置),消耗资源少

缺点:每次升级或更换主题都需要重新修改模板(使用子主题的话可能不需要每次升级都修改)


三.修改主题 functions.php 文件

注意:只适用于 PHP 5.2+

我的需求就是在 WordPress 后台中的“小工具”里增加一个“随机文章”的可用小工具,让我可以将其添加到边栏中作为微件显示出来。

现在我的 WordPress 中插件已经很多了,不希望增加太多的插件造成更多负担,所以就在主题的 functions.php 文件中增加了一个函数类来完成这个任务。

将下面的代码直接,或定制化修改后放入主题的 functions.php 文件中即可。如果当前使用的主题没有 functions.php 文件,可以在该主题的目录下自己创建一个,不过一定要注意在文档的开头输入 <?php,结尾输入 ?>。

成功后在 WordPress 后台的“小工具”页面可以找到对应的可用小工具(左图),添加到边栏后可设置微件标题和显示文章数目(右图)。页面显示效果如本页右上。

代码( // 后为注释):

  1. /**
  2. * Random_Posts widget class
  3. *
  4. * Author: haoxian_zeng <http://cnzhx.net/>
  5. * Date: 2013.05.14, cnzhx2011 1.0
  6. */
  7. //--------------- * 注册该微件
  8. class WP_Widget_myRandom_Posts extends WP_Widget {

  9.     function __construct() {
  10.         $widget_ops = array('classname' => 'widget_my_random_posts', 'description' => __( '水景一页定制的随机文章微件。The cnzhx customized random posts widget.' ) );
  11.         parent::__construct('random-posts', __('随机文章 Random Posts'), $widget_ops);
  12.         $this->alt_option_name = 'widget_my_random_posts';
  13.     }

  14.     function widget( $args, $instance ) {
  15.         global $randomposts, $post;

  16.         extract($args, EXTR_SKIP);
  17.         $output = '';
  18.         // 设置 widget 标题
  19.         $title = apply_filters('widget_title', empty($instance['title']) ? __('随机文章 Random Posts') : $instance['title']);

  20.         // 设置要获取的文章数目
  21.         if ( ! $number = absint( $instance['number'] ) )
  22.             $number = 5;

  23.         // WP 数据库查询,使用 rand 参数来获取随机的排序,并取用前面的 $number 个文章
  24.         $randomposts = get_posts( array( 'number' => $number, 'orderby' => 'rand', 'post_status' => 'publish' ) );

  25.         // 下面开始准备输出数据
  26.         // 先输出一般的 widget 前缀
  27.         $output .= $before_widget;
  28.         // 输出标题
  29.         if ( $title )
  30.         $output .= $before_title . $title . $after_title;

  31.         // random posts 列表开始
  32.         $output .= '<ul id="randomposts">';
  33.         if ( $randomposts ) {
  34.             foreach ( (array) $randomposts as $post) {
  35.                 $output .= '<li><a href="' . get_permalink() . '">' . $post->post_title . '</a></li>';
  36.             }
  37.         }
  38.         $output .= '</ul>';
  39.         // 输出一般的 widget 后缀
  40.         $output .= $after_widget;

  41.         // 输出到页面
  42.         echo $output;
  43.     }

  44.     function update( $new_instance, $old_instance ) {
  45.         $instance = $old_instance;
  46.         $instance['title'] = strip_tags($new_instance['title']);
  47.         $instance['number'] = absint( $new_instance['number'] );

  48.         $alloptions = wp_cache_get( 'alloptions', 'options' );
  49.         if ( isset($alloptions['widget_my_random_posts']) )
  50.             delete_option('widget_my_random_posts');

  51.         return $instance;
  52.     }

  53.     //
  54.     // 在 WP 后台的 widget 内部显示两个参数, 1. 标题;2. 显示文章数目
  55.     //
  56.     function form( $instance ) {
  57.         $title = isset($instance['title']) ? esc_attr($instance['title']) : '';
  58.         $number = isset($instance['number']) ? absint($instance['number']) : 5;
  59.         ?>
  60.         <p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?></label>
  61.         <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>

  62.         <p><label for="<?php echo $this->get_field_id('number'); ?>"><?php _e('Number of posts to show:'); ?></label>
  63.         <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>
  64.         <?php
  65.     }
  66. }

  67. // register WP_Widget_myRandom_Posts widget
  68. add_action( 'widgets_init', create_function( '', 'return register_widget("WP_Widget_myRandom_Posts");' ) );
复制代码



优点:比方法 1 费资源、比方法 2 省资源,可定制化程度极高

缺点:麻烦
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回顶部 返回列表