Trong một số bài viết của tôi hiện nay, tôi đã được yêu cầu tạo một chức năng để trong mỗi bài viết hiển thị số lượt xem, mặc định trên các child theme của genesis đều không có chức năng hiển thị số lượt xem bài viết, vậy đối với nhưng blogger chúng ta việc biết có bao nhiêu người đã truy cập vào trang web của mình trở lên khó khăn hơn. Và bài viết này tôi sẽ hướng dẫn các bạn có thể thêm số lượt xem cụ thể là vào phần info post, hay có thể là entry post trong genesis một cách đơn giản.
Xem thêm Thêm chức năng đếm bình luận trong genesis framework
Đầu tiên chúng ta cần thêm những mã sau để kích hoạt lượt xem bài viết hoạt động.
Kích hoạt lượt xem vào genesis framework
Copy toàn bộ mã bên dưới dán vào functions.php
// Add Post Views on Genesis Framework /** * Set Post Views * It counts everytime single posts is viewed */ if ( !function_exists( 'ja_setPostViews' ) ){ function ja_setPostViews( $postID ){ $count_key = 'ja_post_views'; $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 ); } } } /** * Get the number of views */ if ( !function_exists( 'ja_getPostViews' ) ){ function ja_getPostViews( $postID ){ $count_key = 'ja_post_views'; $count = get_post_meta( $postID, $count_key, true ); if( $count == '0' || $count == '' ){ delete_post_meta( $postID, $count_key ); add_post_meta( $postID, $count_key, '0' ); $label = ''; return; }else{ $label = ( $count == '1' ) ? __( ' View', 'text-domain' ) : __( ' Views', 'text-domain' ); } return $count.$label; } } /** * Set the post view code in single page */ add_action( 'genesis_before_loop', 'ja_SinglePostView' ); function ja_SinglePostView(){ if( is_single() ){ ja_setPostViews( get_the_ID() ); } } /** * Post View Shortcode */ if ( !function_exists( 'ja_PostView_shortcode' ) ){ function ja_PostView_shortcode( $atts, $content = null ){ extract( shortcode_atts( array(), $atts, 'postview' )); return '<span class="ja_post_view" >'.ja_getPostViews( get_the_ID() ).'</span>'; } add_shortcode( 'postview', 'ja_PostView_shortcode' ); }
Và bây giờ lượt xem đã hiển thị trên trang web của bạn, tiếp theo chúng ta sẽ thêm lượt xem vào post info.
Thêm lượt xem vào post info
Chép toàn bộ mã dán vào functions.php
// Add Post Views on Post Info add_filter( 'genesis_post_info', 'ja_add_post_view_to_info' ); function ja_add_post_view_to_info($post_info) { if ( !is_page() ) { $post_info = '[post_date] by [post_author_posts_link] [post_comments] [postview] [post_edit]'; return $post_info; } }
Web hiển thị shortcode bị lỗi các bạn truy cập vào đây để copy Full code tại đây
Thêm lượt xem vào post meta
Sao chép tạo bộ mã và dán vào functions.php
// Add Post Views on Post Meta add_filter( 'genesis_post_meta', 'ja_add_post_view_to_meta' ); function ja_add_post_view_to_meta( $post_meta ) { if ( !is_page() ) { $post_meta = '[post_categories before="Filed Under: "] [post_tags before="Tagged: "] [postview]'; return $post_meta; } }
Web hiển thị shortcode bị lỗi các bạn truy cập vào đây để copy Full code tại đây
Lưu ý: Nếu bạn đang sử dụng plugin Simple Edits của genesis thì có thể thêm đoạn shortcode này vào nơi muốn nó hiển thị [postview] xóa dấu cách.
Hoặc có thể thêm đoạn mã này vào bất cư nơi đâu trong functions.php của bạn.
<?php echo ja_getPostViews( get_the_ID() ); ?>
Và đó là những gì chúng ta cần để thêm lượt xem vào mỗi bài viết trong genesis theme, hãy quay lại trang web của bạn và ấn f5 để tận hưởng thành quả nào.
Hãy cho tôi biết thắc mắc của bạn bên dưới khung comment!