Đếm số lượt xem cho bài viết

Nguyen Eric
0
08/12/24
08/12/2024
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 "01 lượt xem";
    }
    return $count.' lượt xem';
}

// code đếm lượt xem
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);
    }
}

// code hiển thị số lượt xem trong dashboard
add_filter('manage_posts_columns', 'posts_column_views');
add_action('manage_posts_custom_column', 'posts_custom_column_views',5,2);
function posts_column_views($defaults){
    $defaults['post_views'] = __('Views');
    return $defaults;
}
function posts_custom_column_views($column_name, $id){
    if($column_name === 'post_views'){
        echo getPostViews(get_the_ID());
    }
}

function action_woocommerce_single_product_summary(  ) {
echo '';
echo getPostViews(get_the_ID());
echo '';
}
add_action( 'woocommerce_single_product_summary', 'action_woocommerce_single_product_summary', 5, 0 );

add_action('woocommerce_after_shop_loop_item_title', 'mt_view_product_sold', 11);
function mt_view_product_sold()
{
global $product;
$stock = $product->get_stock_quantity();
$buy = get_post_meta($product->get_id(), 'total_sales', true);
 
$round = 0;

Bài viết này được đăng trong Worpress và được gắn thẻ .
Tags :
Tags: