<?php
// ثبت کردن بازدیدها
function dwt_track_post_views ($post_id) {
if ( !is_single() ) return;
if ( empty ( $post_id) ) {
global $post;
$post_id = $post->ID;
}
dwt_set_post_views($post_id);
}
add_action( 'wp_head', 'dwt_track_post_views');
// تنظیم کردن و گرفتن بازدیدها
function dwt_set_post_views($post_id) {
$count_key = '_dwt_post_views_count';
$count = get_post_meta($post_id, $count_key, true);
if($count==''){
$count = 0;
delete_post_meta($post_id, $count_key);
add_post_meta($post_id, $count_key, '1');
}else{
$count++;
update_post_meta($post_id, $count_key, $count);
}
}
// نمایش تعداد بازدیدها
function dwt_get_post_views($post_id){
$count_key = '_dwt_post_views_count';
$count = get_post_meta($post_id, $count_key, true);
if($count==''){
delete_post_meta($post_id, $count_key);
add_post_meta($post_id, $count_key, '0');
return "0";
}
return $count;
}