Skip to content

Instantly share code, notes, and snippets.

@billerickson
Created February 23, 2012 01:07
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save billerickson/1888923 to your computer and use it in GitHub Desktop.
Save billerickson/1888923 to your computer and use it in GitHub Desktop.
Popular Posts Widget
<?php
/**
* Popular Posts Widget
*
* Displays posts in last X days, sorted by most comments (proxy for popularity)
*
* @package Core Functionality
* @author Bill Erickson <bill@billerickson.net>
* @copyright Copyright (c) 2011, Bill Erickson
* @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
*/
/** Add our function to the widgets_init hook. **/
add_action( 'widgets_init', 'be_popular_load_widgets' );
function be_popular_load_widgets() {
register_widget( 'BE_Popular_Widget' );
}
/** Define the Widget as an extension of WP_Widget **/
class BE_Popular_Widget extends WP_Widget {
function BE_Popular_Widget() {
/* Widget settings. */
$widget_ops = array( 'classname' => 'widget_popular', 'description' => 'Displays most popular posts by comment count' );
/* Widget control settings. */
$control_ops = array( 'id_base' => 'popular-widget' );
/* Create the widget. */
$this->WP_Widget( 'popular-widget', 'Popular Posts', $widget_ops, $control_ops );
}
// Limit to last 30 days
function filter_where( $where = '' ) {
// posts in the last 30 days
$where .= " AND post_date > '" . date('Y-m-d', strtotime('-' . $instance['days'] .' days')) . "'";
return $where;
}
function widget( $args, $instance ) {
extract( $args );
echo $before_widget;
if( !empty( $instance['title'] ) ) echo $before_title . $instance['title'] . $after_title;
$loop_args = array(
'posts_per_page' => (int) $instance['count'],
'orderby' => 'comment_count'
);
if( 0 == $instance['days'] ) {
$loop = new WP_Query( $loop_args );
}else{
add_filter( 'posts_where', array( $this, 'filter_where' ) );
$loop = new WP_Query( $loop_args );
remove_filter( 'posts_where', array( $this, 'filter_where' ) );
}
if( $loop->have_posts() ): while( $loop->have_posts() ): $loop->the_post(); global $post;
if( 0 == $loop->current_post )
echo '<p class="popular-post first">';
else
echo '<p class="popular-post">';
echo '<a class="image" href="' . get_permalink() . '">' . genesis_get_image( array( 'size' => 'be_small' ) ) . '</a>';
echo '<span class="date">' . get_the_date() . '</span>';
echo '<a class="title" href="' . get_permalink() . '">' . get_the_title() . '</a>';
echo '</p>';
endwhile; endif; wp_reset_query();
echo $after_widget;
}
function update( $new_instance, $old_instance ) {
$instance = $old_instance;
/* Strip tags (if needed) and update the widget settings. */
$instance['title'] = esc_attr( $new_instance['title'] );
$instance['count'] = (int) $new_instance['count'];
$instance['days'] = (int) $new_instance['days'];
return $instance;
}
function form( $instance ) {
/* Set up some default widget settings. */
$defaults = array( 'title' => '', 'count' => 5, 'days' => 30 );
$instance = wp_parse_args( (array) $instance, $defaults ); ?>
<p>
<label for="<?php echo $this->get_field_id( 'title' ); ?>">Title:</label>
<input id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" value="<?php echo $instance['title']; ?>" />
</p>
<p>
<label for="<?php echo $this->get_field_id( 'count' ); ?>">Number of Posts:</label>
<input id="<?php echo $this->get_field_id( 'count' ); ?>" name="<?php echo $this->get_field_name( 'count' ); ?>" size="3" value="<?php echo $instance['count']; ?>" />
</p>
<p>
<label for="<?php echo $this->get_field_id( 'days' ); ?>">Posted in the past X days:</label>
<input id="<?php echo $this->get_field_id( 'days' ); ?>" name="<?php echo $this->get_field_name( 'days' ); ?>" size="3" value="<?php echo $instance['days']; ?>" />
</p>
<p class="description">Use 0 for no time limit.</p>
<?php
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment