ที่แล้วๆ มา ผมใช้ WordPress ในการทำเว็บอะไรก็แล้วแต่ เน้นไปที่ความสะดวกสบายไว้ก่อน เช่น การใช้งาน Plugin เพิ่มความสะดวกสบายให้กับตัวเอง แต่พอนานๆ เข้า เริ่มไม่คิดแบบนั้นแล้ว เพราะว่า ยิ่งเรามี Plugin ในระบบมากเท่าไร เว็บเราก็จะช้า และกินทรัพยากรของโฮสมากเท่านั้น และยังเป็นสาเหตุของการถูกแฮคอีกด้วย
วันนี้ ผมเอาฟังก์ชั่น wordpress นับจำนวนคนอ่าน บทความแบบง่ายๆ มาให้เอาไปใช้กันครับ โดยที่คุณจะลืมไปเลยว่า Plugin ที่ชื่อ wp_pageview นั้น หน้าตาเป็นอย่างไร
เปิดไฟล์ functions.php ขึ้นมา แล้วใส่โค้ดด้านล่างนี้ลงไปครับ
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, '1');
        return '1 View';
    }
    return $count.' Views';
}
// function to count views.
function setPostViews($postID) {
    $count_key = 'post_views_count';
    $count = get_post_meta($postID, $count_key, true);
    if($count==''){
        $count = 1;
        delete_post_meta($postID, $count_key);
        add_post_meta($postID, $count_key, '1');
    }else{
        $count++;
        update_post_meta($postID, $count_key, $count);
    }
}
// Add it to a column in WP-Admin - (Optional)
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());
    }
}
จากโค้ดด้านบน จะอธิบายคร่าวๆดังนี้นะครับ
- ฟังก์ชั่น setPostViews() จะทำการ “นับ” หากเราเปิดหน้าบทความนั้นๆขึ้นมา
- ฟังก์ชั่น getPostViews() จะทำการ “แสดง” ค่าที่นับเข้ามา
งงมั๊ยเอ่ย ถ้างง มาดูกันต่อ
การใช้งานฟังก์ชั่นทั้งสอง setPostViews() และ getPostViews()
ผมยกตัวอย่างในไฟล์ single.php ละกัน
<?php if (have_posts()) : ?> 
<?php setPostViews(get_the_ID()); ?>
<div class="grid_8_bg">
	<?php while (have_posts()) : the_post(); ?> 
	<div class="content">
		<div class="page-breadcrumbs"><?php if(function_exists('bcn_display')){ bcn_display(); }?></div>
		<div class="page-title"><h2><?php the_title(); ?></h2></div>
		<div class="page-detail">
                     <?php echo human_time_diff( get_the_time('U'), current_time('timestamp') ) . ' ago'; ?> </b> (<?php echo getPostViews(get_the_ID()); ?>)
                </div>
		<div class="page-content">
			<?php the_content(''); ?>	
		</div>
		<div class="page-content-tag">
			<span>Tags (คำค้นหา)</span>
			<span class="img-tag"><img src="<?php bloginfo('template_url'); ?>/images/tags.png"/></span><p><?php the_tags( '', ', ', ''); ?></p>
		</div>
		<div class="clear"></div>
	</div>
	<?php endwhile; ?>
</div>
<?php else : ?>
<div>
     <div style="width:640px;text-align:center;padding:50px 10px;font-size:24px;">ขออภัย! ยังไม่มีเนื้อหาสำหรับหมวดนี้....</div>
</div>
<?php  endif; ?>
ลองดูตัวอย่างแล้วงงอีก คอมเม้นต์ถามกันมาได้นะครับ สำหรับการใช้ฟังก์ชั่นทั้งสองนี้ แล้วคุณจะไม่ต้องนึกถึง plugin wp_pageview อีกเลย ลองเอาไปใช้กันดูสำหรับ WordPress : นับจำนวนคนอ่านบทความ แบบไม่ใช้ Plugin
บทความโดย : tsupaman
 


 
 
                                        
 
                                        
 
                                        
 
                                        
 
                                        
 
                                        
 
                                        
 
                                        
 
                                        
 
                                        
 
                                        
Comments