/** * @param array $args * @param array $instance */ public function widget($args, $instance) { $cache = array(); if (!$this->is_preview()) { $cache = hq_cache_get('widget_recent_posts', 'widget'); } if (!is_array($cache)) { $cache = array(); } if (!isset($args['widget_id'])) { $args['widget_id'] = $this->id; } if (isset($cache[$args['widget_id']])) { echo $cache[$args['widget_id']]; return; } ob_start(); $title = !empty($instance['title']) ? $instance['title'] : __('Recent Posts'); /** This filter is documented in hq-includes/default-widgets.php */ $title = apply_filters('widget_title', $title, $instance, $this->id_base); $number = !empty($instance['number']) ? absint($instance['number']) : 5; if (!$number) { $number = 5; } $show_date = isset($instance['show_date']) ? $instance['show_date'] : false; /** * Filter the arguments for the Recent Posts widget. * * @since 0.0.1 * * @see HQ_Query::get_posts() * * @param array $args An array of arguments used to retrieve the recent posts. */ $r = new HQ_Query(apply_filters('widget_posts_args', array('posts_per_page' => $number, 'no_found_rows' => true, 'post_status' => 'publish', 'ignore_sticky_posts' => true))); if ($r->have_posts()) { ?> <?php echo $args['before_widget']; ?> <?php if ($title) { echo $args['before_title'] . $title . $args['after_title']; } ?> <ul> <?php while ($r->have_posts()) { $r->the_post(); ?> <li> <a href="<?php the_permalink(); ?> "><?php get_the_title() ? the_title() : the_ID(); ?> </a> <?php if ($show_date) { ?> <span class="post-date"><?php echo get_the_date(); ?> </span> <?php } ?> </li> <?php } ?> </ul> <?php echo $args['after_widget']; // Reset the global $the_post as this query will have stomped on it hq_reset_postdata(); } if (!$this->is_preview()) { $cache[$args['widget_id']] = ob_get_flush(); hq_cache_set('widget_recent_posts', $cache, 'widget'); } else { ob_end_flush(); } }
/** * Destroy the previous query and set up a new query. * * This should be used after {@link query_posts()} and before another {@link * query_posts()}. This will remove obscure bugs that occur when the previous * hq_query object is not destroyed properly before another is set up. * * @since 0.0.1 * * @global HQ_Query $hq_query * @global HQ_Query $hq_the_query */ function hq_reset_query() { $GLOBALS['hq_query'] = $GLOBALS['hq_the_query']; hq_reset_postdata(); }