Viết Widget Top 10 Bài Viết Được Xem Nhiều Nhất (ok)
https://fcwordpress.net/viet-widget-top-10-bai-viet-duoc-xem-nhieu-nhat.html
Last updated
https://fcwordpress.net/viet-widget-top-10-bai-viet-duoc-xem-nhieu-nhat.html
Last updated
count_counter_top_posts.php
<?php
function setPostViews($postID) {
$count_key = 'post_views_count';
$count = get_post_meta($postID, $count_key, true);
if($count==''){
$count = 0;
delete_post_meta($postID, $count_key);
add_post_meta($postID, $count_key, '0');
}else{
$count++;
update_post_meta($postID, $count_key, $count);
}
}
function getPostViews($postID){
$count_key = 'post_views_count';
$count = get_post_meta($postID, $count_key, true);
if($count==''){
delete_post_meta($postID, $count_key);
add_post_meta($postID, $count_key, '0');
return "0";
}
return $count;
}
class top_view_posts extends WP_Widget {
function __construct(){
$widget_ops = array('description' => 'Lionel Tin đọc nhiều nhất');
$control_ops = array('width' => 300, 'height' => 300);
parent::__construct(false,$name='Tin đọc nhiều nhất',$widget_ops,$control_ops);
}
function form($instance){
global $wpdb;
$instance = wp_parse_args( (array) $instance, array('title'=>'','numbershow'=>'') );
$title = htmlspecialchars($instance['title']);
$numbershow = htmlspecialchars($instance['numbershow']);
?>
<p>
<label for="<?php echo $this->get_field_id('title'); ?>">Title </label>
<input type="text" name="<?php echo $this->get_field_name('title'); ?>" id="<?php echo $this->get_field_id('title'); ?>" style="width:100%" value="<?php echo $title; ?>">
</p>
<p>
<label for="<?php echo $this->get_field_id('numbershow'); ?>">Show number: </label>
<input type="text" name="<?php echo $this->get_field_name('numbershow'); ?>" id="<?php echo $this->get_field_id('numbershow'); ?>" style="width:100%" value="<?php echo $numbershow; ?>">
</p>
<?php
}
function update($new_instance, $old_instance){
$instance = $old_instance;
$instance['title'] = stripslashes($new_instance['title']);
$instance['numbershow'] = stripslashes($new_instance['numbershow']);
return $instance;
}
function widget($args, $instance) {
global $wpdb,$post, $product;
extract($args);
$title= empty($instance['title']) ? '' : $instance['title'];
$numbershow= empty($instance['numbershow']) ? '' : $instance['numbershow'];
echo $before_widget;
if ( ! empty( $instance['title'] ) ) {
echo $args['before_title'] . apply_filters( 'widget_title', $instance['title'] ). $args['after_title'];
}
?>
<ul>
<?php
$args = array(
'post_type' => 'post',
'posts_per_page' => $numbershow,
'post_status' => 'publish',
'meta_key' => 'post_views_count',
'orderby' => 'meta_value_num',
'order' => 'DESC'
);
$wp_query = new WP_Query( $args );
if ( $wp_query->have_posts() ) : while ( $wp_query->have_posts() ) : $wp_query->the_post();
?>
<li>
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a>
<span class="getPostViews"><?php echo getPostViews(get_the_ID()); ?> lượt xem</span>
</li>
<?php
endwhile;
wp_reset_postdata();
endif;
?>
</ul>
<?php
echo $after_widget;
}
}
register_widget('top_view_posts');
?>
single.php
setPostViews(get_the_ID());
Đọc thêm: