WordPress教程

随机帖子小工具类别选择分类

阿里云
  1. /**
  2.  * 随机小工具 分类
  3.  *
  4.  * Displays posts from a selected category
  5.  *
  6.  * @since 1.0.0
  7. */class Category_Posts extends WP_Widget 
  8. {
  9.  
  10.     public function __construct() 
  11.     {
  12.         parent::__construct(
  13.             'widget_category_posts', 
  14.             _x( 'Category Posts Widget', 'Category Posts Widget' ), 
  15.             [ 'description' => __( 'Display a list of posts from a selected category.' ) ] 
  16.         );
  17.         $this->alt_option_name = 'widget_category_posts';
  18.  
  19.         add_action( 'save_post', [$this, 'flush_widget_cache'] );
  20.         add_action( 'deleted_post', [$this, 'flush_widget_cache'] );
  21.         add_action( 'switch_theme', [$this, 'flush_widget_cache'] );
  22.     }
  23.  
  24.     public function widget( $args, $instance ) 
  25.     {
  26.         $cache = [];
  27.         if ( ! $this->is_preview() ) {
  28.             $cache = wp_cache_get( 'widget_cat_posts', 'widget' );
  29.         }
  30.  
  31.         if ( ! is_array( $cache ) ) {
  32.             $cache = [];
  33.         }
  34.  
  35.         if ( ! isset( $args['widget_id'] ) ) {
  36.             $args['widget_id'] = $this->id;
  37.         }
  38.  
  39.         if ( isset( $cache[ $args['widget_id'] ] ) ) {
  40.             echo $cache[ $args['widget_id'] ];
  41.             return;
  42.         }
  43.  
  44.         ob_start();
  45.  
  46.         $title          = ( ! empty( $instance['title'] ) ) ? $instance['title'] : __( 'Category Posts' );
  47.         /** This filter is documented in wp-includes/default-widgets.php */        $title          = apply_filters( 'widget_title', $title, $instance, $this->id_base );
  48.         $number         = ( ! empty( $instance['number'] ) ) ? absint( $instance['number'] ) : 5;
  49.         if ( ! $number ) {
  50.             $number = 5;
  51.         }
  52.         $cat_id         = $instance['cat_id'];
  53.         $random         = $instance['rand'] ? true : false; 
  54.         $excerpt        = $instance['excerpt'] ? true : false; 
  55.         $thumbnail      = $instance['thumbnail'] ? true : false; 
  56.  
  57.         /**
  58.          * Filter the arguments for the Category Posts widget.
  59.          *
  60.          * @since 1.0.0
  61.          *
  62.          * @see WP_Query::get_posts()
  63.          *
  64.          * @param array $args An array of arguments used to retrieve the category posts.
  65.          */        if( true === $random ) {
  66.  
  67.             $query_args = [
  68.                 'posts_per_page'    => $number,
  69.                 'cat'               => $cat_id,
  70.                 'orderby'           => 'rand'
  71.             ];
  72.  
  73.         }else{  
  74.  
  75.             $query_args = [
  76.                 'posts_per_page'    => $number,
  77.                 'cat'               => $cat_id,
  78.             ];
  79.  
  80.         }
  81.         $q = new WP_Query( apply_filters( 'category_posts_args', $query_args ) );
  82.  
  83.         if( $q->have_posts() ) {
  84.  
  85.             echo $args['before_widget'];
  86.             if ( $title ) {
  87.                 echo $args['before_title'] . $title . $args['after_title'];
  88.             }               
  89.  
  90.             while( $q->have_posts() ) {
  91.                 $q->the_post(); ?>
  92.  
  93.                 <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>> 
  94.  
  95.                     <header class="entry-header">
  96.                         <?php the_title( '<p class="entry-title"><a href="' . esc_url( get_permalink() ) . '" rel="bookmark">', '</a></p>' ); ?>
  97.                     </header><!-- .entry-header -->
  98.  
  99.                     <?php 
  100.                     if ( has_post_thumbnail() && true === $thumbnail ) { ?>
  101.  
  102.                         <div class="post-thumbnail">
  103.  
  104.                             <?php the_post_thumbnail(); ?>
  105.  
  106.                         </div><!--/.post-thumbnail-->
  107.  
  108.                     <?php   
  109.                     }
  110.  
  111.                     if( true === $excerpt ) { ?>    
  112.  
  113.                         <div class="entry-summary">
  114.                             <?php the_excerpt(); ?>
  115.                         </div><!-- .entry-summary -->
  116.  
  117.                     <?php } ?>
  118.  
  119.                 </article><!-- #post-## -->
  120.  
  121.                 <?php
  122.             }
  123.  
  124.  
  125.             wp_reset_postdata();
  126.         }
  127.             echo $args['after_widget']; 
  128.  
  129.         if ( ! $this->is_preview() ) {
  130.             $cache[ $args['widget_id'] ] = ob_get_flush();
  131.             wp_cache_set( 'widget_cat_posts', $cache, 'widget' );
  132.         } else {
  133.             ob_end_flush();
  134.         }
  135.     }
  136.  
  137.     public function update( $new_instance, $old_instance ) 
  138.     {
  139.         $instance                   = $old_instance;
  140.         $instance['title']          = strip_tags( $new_instance['title'] );
  141.         $instance['number']         = (int) $new_instance['number'];
  142.         $instance['cat_id']         = (int) $new_instance['cat_id'];
  143.         $instance['rand']           = $new_instance['rand'];
  144.         $instance['excerpt']        = $new_instance['excerpt'];
  145.         $instance['thumbnail']      = $new_instance['thumbnail'];
  146.         $this->flush_widget_cache();
  147.  
  148.         $alloptions = wp_cache_get( 'alloptions', 'options' );
  149.         if ( isset($alloptions['widget_category_posts']) )
  150.             delete_option('widget_category_posts');
  151.  
  152.         return $instance;
  153.     }
  154.  
  155.     public function flush_widget_cache() 
  156.     {
  157.         wp_cache_delete('widget_cat_posts', 'widget');
  158.     }
  159.  
  160.     public function form( $instance ) 
  161.     {
  162.  
  163.         $title      = isset( $instance['title'] ) ? esc_attr( $instance['title'] ) : '';
  164.         $number     = isset( $instance['number'] ) ? absint( $instance['number'] ) : 5;
  165.         $cat_id     = isset( $instance['cat_id'] ) ? absint( $instance['cat_id'] ) : 1;
  166.         $random     = isset( $instance['rand'] ) ? $instance['rand'] : false; 
  167.         $excerpt    = isset( $instance['excerpt'] ) ? $instance['excerpt'] : false; 
  168.         $thumbnail  = isset( $instance['thumbnail'] ) ? $instance['thumbnail'] : false; 
  169.         ?>
  170.  
  171.         <p>
  172.             <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></label>
  173.             <input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo $title; ?>" />
  174.         </p>
  175.  
  176.         <p>
  177.             <label for="<?php echo $this->get_field_id( 'number' ); ?>"><?php _e( 'Number of posts to show:' ); ?></label>
  178.             <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" />
  179.         </p>
  180.  
  181.         <p>
  182.             <label for="<?php echo $this->get_field_id('cat_id'); ?>"><?php _e( 'Category Name:' )?></label>
  183.             <select id="<?php echo $this->get_field_id('cat_id'); ?>" name="<?php echo $this->get_field_name('cat_id'); ?>">
  184.                 <?php 
  185.                 $this->categories = get_categories();
  186.                 foreach ( $this->categories as $cat ) {
  187.                     $selected = ( $cat->term_id == esc_attr( $cat_id ) ) ? ' selected = "selected" ' : '';
  188.                     $option = '<option '.$selected .'value="' . $cat->term_id;
  189.                     $option = $option .'">';
  190.                     $option = $option .$cat->name;
  191.                     $option = $option .'</option>';
  192.                     echo $option;
  193.                 }
  194.                 ?>
  195.             </select>
  196.         </p>
  197.  
  198.         <p>
  199.             <label for="<?php echo $this->get_field_id('rand'); ?>"><?php _e( 'Show random posts' ); ?></label>
  200.             <?php $checked = ( $random ) ? ' checked=\"checked\" ' : ''; ?>
  201.             <input type="checkbox" id="<?php echo $this->get_field_id( 'rand' ); ?>" name="<?php echo $this->get_field_name( 'rand' ); ?>" value="true" <?php echo $checked; ?> />    
  202.         </p>
  203.  
  204.         <p>
  205.             <label for="<?php echo $this->get_field_id('excerpt'); ?>"><?php _e( 'Show excerpt. If unchecked, only the title of the post will be displayed' ); ?></label>
  206.             <?php $checked = ( $excerpt ) ? ' checked=\"checked\" ' : ''; ?>
  207.             <input type="checkbox" id="<?php echo $this->get_field_id( 'excerpt' ); ?>" name="<?php echo $this->get_field_name( 'excerpt' ); ?>" value="true" <?php echo $checked; ?> />    
  208.         </p>
  209.  
  210.         <p>
  211.             <label for="<?php echo $this->get_field_id('thumbnail'); ?>"><?php _e( 'Hide post thumbnail' ); ?></label>
  212.             <?php $checked = ( $thumbnail ) ? ' checked=\"checked\" ' : ''; ?>
  213.             <input type="checkbox" id="<?php echo $this->get_field_id( 'thumbnail' ); ?>" name="<?php echo $this->get_field_name( 'thumbnail' ); ?>" value="true" <?php echo $checked; ?> />    
  214.         </p>
  215.  
  216.     <?php
  217.     }
  218.  
  219. }
  220.  
  221. add_action( 'widgets_init', function () 
  222. {
  223.     register_widget( 'Category_Posts' );
  224. });

随机帖子小工具类别选择分类

已有 243 人购买
查看演示升级 VIP立刻购买

也想出现在这里?联系我们
创客主机
收藏
(0)

发表回复

热销模板

Ashade - 作品展示摄影相册WordPress汉化主题
LensNews

本站承接 WordPress / PbootCMS / DedeCMS 等
系统建站、仿站、开发、定制等业务!