!
也想出现在这里? 联系我们
广告位

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

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

给TA打赏
共{{data.count}}人
人已打赏
WordPress教程

WordPress无插件完美实现邮箱SMTP发件功能

2024-7-3 19:30:41

WordPress教程

WordPress使用经验 独立的Description 和 Keywords

2024-7-8 7:30:07

下载说明

  • 1、微码盒所提供的压缩包若无特别说明,解压密码均为weimahe.com
  • 2、下载后文件若为压缩包格式,请安装7Z软件或者其它压缩软件进行解压;
  • 3、文件比较大的时候,建议使用下载工具进行下载,浏览器下载有时候会自动中断,导致下载错误;
  • 4、资源可能会由于内容问题被和谐,导致下载链接不可用,遇到此问题,请到文章页面进行反馈,以便微码盒及时进行更新;
  • 5、其他下载问题请自行搜索教程,这里不一一讲解。

站长声明

本站大部分下载资源收集于网络,只做学习和交流使用,版权归原作者所有;若为付费资源,请在下载后24小时之内自觉删除;若作商业用途,请到原网站购买;由于未及时购买和付费发生的侵权行为,与本站无关。本站发布的内容若侵犯到您的权益,请联系本站删除,我们将及时处理!
0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索