Popular Posts Widget

/* ~~~~~~~~~~~ Popular Posts Widget ~~~~~~~~~~~ */

function register_ter_popular_posts(){ register_widget('TerPopularPosts'); }
add_action('widgets_init', 'register_ter_popular_posts');

function ter_set_post_views($id){
$count_key = 'ter_post_view_count';
$count = get_post_meta($id,$count_key,true);
if($count == ''){
$count = 0;
delete_post_meta($id,$count_key);
add_post_meta($id,$count_key,'0');
}
else{
$count++;
update_post_meta($id,$count_key,$count);
}
}

function ter_get_post_views($id){
$count_key = 'ter_post_view_count';
$count = get_post_meta($id,$count_key,true);
if($count == ''){
delete_post_meta($id,$count_key);
add_post_meta($id,$count_key,'0');
return '0 Views';
}
return $count . ' Views';
}

class TerPopularPosts extends WP_Widget{
function __construct() {
parent::__construct(
'ter_popular_posts',
__('Villages Popular Posts','terra'),
array('description' => __('The Popular Posts Widget','terra'),)
);
}

public function widget($args,$instance){
$title = apply_filters('widget_title',$instance['title']);
echo $args['before_widget'];
if(!empty($title)) echo $args['before_title'] . $title . $args['after_title'];

echo '<ul class="popular-posts">';
query_posts('meta_key=ter_post_view_count&orderby=meta_value_num&order=DESC&showposts=5');
if(have_posts()) : while (have_posts()) : the_post(); ?>
<li><a href="<?php the_permalink() ?>"><?php the_title() ?></a></li>
<?php
endwhile; endif;
wp_reset_query();
echo '</ul>';

echo $args['after_widget'];
}

public function form($instance){
if(isset($instance['title'])) $title = $instance['title'];
else $title = __('Popular Posts','terra');
?>
<p>
<label for="<?php echo $this->get_field_id('title') ?>"><?php _e( 'Title:' ) ?></label>
<input class="widefat" id="<?php echo $this->get_field_id('title') ?>" name="<?php echo $this->get_field_name('title') ?>" type="text" value="<?php echo esc_attr($title); ?>">
</p>
<?php
}

public function update($new_instance,$old_instance){
$instance = array();
$instance['title'] = (! empty($new_instance['title'])) ? strip_tags($new_instance['title']) : '';
return $instance;
}

}//END TerPopularPosts

0 Comments

You must be logged in to post a comment.