Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 
 

143 Zeilen
4.7 KiB

  1. <?php
  2. /**
  3. * Widget API: WP_Widget_Recent_Posts class
  4. *
  5. * @package WordPress
  6. * @subpackage Widgets
  7. * @since 4.4.0
  8. */
  9. /**
  10. * Core class used to implement a Recent Posts widget.
  11. *
  12. * @since 2.8.0
  13. *
  14. * @see WP_Widget
  15. */
  16. class WP_Widget_Recent_Posts extends WP_Widget {
  17. /**
  18. * Sets up a new Recent Posts widget instance.
  19. *
  20. * @since 2.8.0
  21. * @access public
  22. */
  23. public function __construct() {
  24. $widget_ops = array(
  25. 'classname' => 'widget_recent_entries',
  26. 'description' => __( 'Your site&#8217;s most recent Posts.' ),
  27. 'customize_selective_refresh' => true,
  28. );
  29. parent::__construct( 'recent-posts', __( 'Recent Posts' ), $widget_ops );
  30. $this->alt_option_name = 'widget_recent_entries';
  31. }
  32. /**
  33. * Outputs the content for the current Recent Posts widget instance.
  34. *
  35. * @since 2.8.0
  36. * @access public
  37. *
  38. * @param array $args Display arguments including 'before_title', 'after_title',
  39. * 'before_widget', and 'after_widget'.
  40. * @param array $instance Settings for the current Recent Posts widget instance.
  41. */
  42. public function widget( $args, $instance ) {
  43. if ( ! isset( $args['widget_id'] ) ) {
  44. $args['widget_id'] = $this->id;
  45. }
  46. $title = ( ! empty( $instance['title'] ) ) ? $instance['title'] : __( 'Recent Posts' );
  47. /** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */
  48. $title = apply_filters( 'widget_title', $title, $instance, $this->id_base );
  49. $number = ( ! empty( $instance['number'] ) ) ? absint( $instance['number'] ) : 5;
  50. if ( ! $number )
  51. $number = 5;
  52. $show_date = isset( $instance['show_date'] ) ? $instance['show_date'] : false;
  53. /**
  54. * Filters the arguments for the Recent Posts widget.
  55. *
  56. * @since 3.4.0
  57. *
  58. * @see WP_Query::get_posts()
  59. *
  60. * @param array $args An array of arguments used to retrieve the recent posts.
  61. */
  62. $r = new WP_Query( apply_filters( 'widget_posts_args', array(
  63. 'posts_per_page' => $number,
  64. 'no_found_rows' => true,
  65. 'post_status' => 'publish',
  66. 'ignore_sticky_posts' => true
  67. ) ) );
  68. if ($r->have_posts()) :
  69. ?>
  70. <?php echo $args['before_widget']; ?>
  71. <?php if ( $title ) {
  72. echo $args['before_title'] . $title . $args['after_title'];
  73. } ?>
  74. <ul>
  75. <?php while ( $r->have_posts() ) : $r->the_post(); ?>
  76. <li>
  77. <a href="<?php the_permalink(); ?>"><?php get_the_title() ? the_title() : the_ID(); ?></a>
  78. <?php if ( $show_date ) : ?>
  79. <span class="post-date"><?php echo get_the_date(); ?></span>
  80. <?php endif; ?>
  81. </li>
  82. <?php endwhile; ?>
  83. </ul>
  84. <?php echo $args['after_widget']; ?>
  85. <?php
  86. // Reset the global $the_post as this query will have stomped on it
  87. wp_reset_postdata();
  88. endif;
  89. }
  90. /**
  91. * Handles updating the settings for the current Recent Posts widget instance.
  92. *
  93. * @since 2.8.0
  94. * @access public
  95. *
  96. * @param array $new_instance New settings for this instance as input by the user via
  97. * WP_Widget::form().
  98. * @param array $old_instance Old settings for this instance.
  99. * @return array Updated settings to save.
  100. */
  101. public function update( $new_instance, $old_instance ) {
  102. $instance = $old_instance;
  103. $instance['title'] = sanitize_text_field( $new_instance['title'] );
  104. $instance['number'] = (int) $new_instance['number'];
  105. $instance['show_date'] = isset( $new_instance['show_date'] ) ? (bool) $new_instance['show_date'] : false;
  106. return $instance;
  107. }
  108. /**
  109. * Outputs the settings form for the Recent Posts widget.
  110. *
  111. * @since 2.8.0
  112. * @access public
  113. *
  114. * @param array $instance Current settings.
  115. */
  116. public function form( $instance ) {
  117. $title = isset( $instance['title'] ) ? esc_attr( $instance['title'] ) : '';
  118. $number = isset( $instance['number'] ) ? absint( $instance['number'] ) : 5;
  119. $show_date = isset( $instance['show_date'] ) ? (bool) $instance['show_date'] : false;
  120. ?>
  121. <p><label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></label>
  122. <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; ?>" /></p>
  123. <p><label for="<?php echo $this->get_field_id( 'number' ); ?>"><?php _e( 'Number of posts to show:' ); ?></label>
  124. <input class="tiny-text" id="<?php echo $this->get_field_id( 'number' ); ?>" name="<?php echo $this->get_field_name( 'number' ); ?>" type="number" step="1" min="1" value="<?php echo $number; ?>" size="3" /></p>
  125. <p><input class="checkbox" type="checkbox"<?php checked( $show_date ); ?> id="<?php echo $this->get_field_id( 'show_date' ); ?>" name="<?php echo $this->get_field_name( 'show_date' ); ?>" />
  126. <label for="<?php echo $this->get_field_id( 'show_date' ); ?>"><?php _e( 'Display post date?' ); ?></label></p>
  127. <?php
  128. }
  129. }