ยินดีต้อนรับคุณ, บุคคลทั่วไป กรุณา เข้าสู่ระบบ หรือ ลงทะเบียน

เข้าสู่ระบบด้วยชื่อผู้ใช้ รหัสผ่าน และระยะเวลาในเซสชั่น

ThaiSEOBoard.comพัฒนาเว็บไซต์CMS & Free Script[ถาม]วิธีทำให้แต่ละบทความแสดงจำนวน View
หน้า: [1]   ลงล่าง
พิมพ์
ผู้เขียน หัวข้อ: [ถาม]วิธีทำให้แต่ละบทความแสดงจำนวน View  (อ่าน 647 ครั้ง)
0 สมาชิก และ 1 บุคคลทั่วไป กำลังดูหัวข้อนี้
darkmb
Newbie
*

พลังน้ำใจ: 1
ออฟไลน์ ออฟไลน์

กระทู้: 28



ดูรายละเอียด
« เมื่อ: 21 พฤศจิกายน 2016, 01:20:57 »

ตอนนี้ใช้ปลั๊กอิน WP-Post View อยู่ค่ะ แต่มันแสดงผล View แค่ในหน้าต่างควบคุมของทาง Admin เท่านั้น อยากทราบว่าทำยังไงถึงจะแสดง View ของแต่ละบทความได้คะ ?
ตัวอย่างอยากได้ประมาณนี้ค่ะ



ขอบคุณทุกความเห็นค่ะ
บันทึกการเข้า
manisakunrat
คนรักเสียว
*

พลังน้ำใจ: 1
ออฟไลน์ ออฟไลน์

กระทู้: 135



ดูรายละเอียด
« ตอบ #1 เมื่อ: 21 พฤศจิกายน 2016, 01:25:47 »

รอผู้รู้มาตอบคะ
บันทึกการเข้า
(~''๐''~)ปลาแซลมอล
หัวหน้าแก๊งเสียว
*

พลังน้ำใจ: 60
ออฟไลน์ ออฟไลน์

กระทู้: 1,139



ดูรายละเอียด
« ตอบ #2 เมื่อ: 21 พฤศจิกายน 2016, 10:18:07 »

ถ้าจำไม่ผิด Plug in ตัวนี้ใช้คำสั่ง
โค๊ด:
 %VIEW_COUNT% views
ใส่ไปยังตำแหน่งที่ต้องการนะครับ
บันทึกการเข้า
darkknightza
เจ้าพ่อบอร์ดเสียว
*

พลังน้ำใจ: 167
ออฟไลน์ ออฟไลน์

กระทู้: 4,245



ดูรายละเอียด เว็บไซต์
« ตอบ #3 เมื่อ: 21 พฤศจิกายน 2016, 10:21:13 »

ถ้าจำไม่ผิด Plug in ตัวนี้ใช้คำสั่ง
โค๊ด:
 %VIEW_COUNT% views
ใส่ไปยังตำแหน่งที่ต้องการนะครับ
เทพมากครับ wanwan017
บันทึกการเข้า

หาเงินวันละ350บาท มั่นคง จ่ายมาสิบปีแล้ว
หารายได้กับ popup เจ้านี้ เรทแรงคลิ๊ก
Hosting อันดับ 1 คุณภาพสูง ราคาถูก จัดเลย
โดเมนเนมสวยๆ ราคาถูก จดกับเราสิที่นี่
darkmb
Newbie
*

พลังน้ำใจ: 1
ออฟไลน์ ออฟไลน์

กระทู้: 28



ดูรายละเอียด
« ตอบ #4 เมื่อ: 21 พฤศจิกายน 2016, 14:41:35 »

ถ้าจำไม่ผิด Plug in ตัวนี้ใช้คำสั่ง
โค๊ด:
 %VIEW_COUNT% views
ใส่ไปยังตำแหน่งที่ต้องการนะครับ

ขอบคุณมากค่ะ ว่าแต่ต้องแทรกลงไปในไฟล์ไหนหรือว่าส่วนไหนหรอคะ ขอรบกวนด้วยค่ะ  wanwan017
บันทึกการเข้า
ThaiToday
คนรักเสียว
*

พลังน้ำใจ: 6
ออฟไลน์ ออฟไลน์

กระทู้: 120



ดูรายละเอียด
« ตอบ #5 เมื่อ: 21 พฤศจิกายน 2016, 14:43:09 »

ติดตามด้วยยคน
บันทึกการเข้า
nos
ก๊วนเสียว
*

พลังน้ำใจ: 7
ออฟไลน์ ออฟไลน์

กระทู้: 281



ดูรายละเอียด
« ตอบ #6 เมื่อ: 11 ธันวาคม 2016, 02:11:02 »

ตอนนี้ใช้ปลั๊กอิน WP-Post View อยู่ค่ะ แต่มันแสดงผล View แค่ในหน้าต่างควบคุมของทาง Admin เท่านั้น อยากทราบว่าทำยังไงถึงจะแสดง View ของแต่ละบทความได้คะ ?
ตัวอย่างอยากได้ประมาณนี้ค่ะ



ขอบคุณทุกความเห็นค่ะ
'''



เพิ่มโค๊ดด้านล่างนี้เข้าไปในไฟล์ function.php เป็น function สำหรับ อัพเดทจำนวน views และแสดงจำนวน views
<?php
/************************************CODE-1***************************************
* @Author: Boutros AbiChedid
* @Date:   January 16, 2012
* @Websites: http://bacsoftwareconsulting.com/ ; http://blueoliveonline.com/
* @Description: Displays the Number of times Posts are Viewed on Your Blog.
* Function: Sets, Tracks and Displays the Count of Post Views (Post View Counter)
* Code is browser and JavaScript independent.
* @Tested on: WordPress version 3.2.1
*********************************************************************************/
//Set the Post Custom Field in the WP dashboard as Name/Value pair
function bac_PostViews($post_ID) {
 
    //Set the name of the Posts Custom Field.
    $count_key = 'post_views_count';
     
    //Returns values of the custom field with the specified key from the specified post.
    $count = get_post_meta($post_ID, $count_key, true);
     
    //If the the Post Custom Field value is empty.
    if($count == ''){
        $count = 0; // set the counter to zero.
         
        //Delete all custom fields with the specified key from the specified post.
        delete_post_meta($post_ID, $count_key);
         
        //Add a custom (meta) field (Name/value)to the specified post.
        add_post_meta($post_ID, $count_key, '0');
        return $count . ' View';
     
    //If the the Post Custom Field value is NOT empty.
    }else{
        $count++; //increment the counter by 1.
        //Update the value of an existing meta key (custom field) for the specified post.
        update_post_meta($post_ID, $count_key, $count);
         
        //If statement, is just to have the singular form 'View' for the value '1'
        if($count == '1'){
        return $count . ' View';
        }
        //In all other cases return (count) Views
        else {
        return $count . ' Views';
        }
    }
}
?>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
   
<?php
/************************************CODE-1***************************************
* @Author: Boutros AbiChedid
* @Date:   January 16, 2012
* @Websites: http://bacsoftwareconsulting.com/ ; http://blueoliveonline.com/
* @Description: Displays the Number of times Posts are Viewed on Your Blog.
* Function: Sets, Tracks and Displays the Count of Post Views (Post View Counter)
* Code is browser and JavaScript independent.
* @Tested on: WordPress version 3.2.1
*********************************************************************************/
//Set the Post Custom Field in the WP dashboard as Name/Value pair
function bac_PostViews($post_ID) {
 
    //Set the name of the Posts Custom Field.
    $count_key = 'post_views_count';
     
    //Returns values of the custom field with the specified key from the specified post.
    $count = get_post_meta($post_ID, $count_key, true);
     
    //If the the Post Custom Field value is empty.
    if($count == ''){
        $count = 0; // set the counter to zero.
         
        //Delete all custom fields with the specified key from the specified post.
        delete_post_meta($post_ID, $count_key);
         
        //Add a custom (meta) field (Name/value)to the specified post.
        add_post_meta($post_ID, $count_key, '0');
        return $count . ' View';
     
    //If the the Post Custom Field value is NOT empty.
    }else{
        $count++; //increment the counter by 1.
        //Update the value of an existing meta key (custom field) for the specified post.
        update_post_meta($post_ID, $count_key, $count);
         
        //If statement, is just to have the singular form 'View' for the value '1'
        if($count == '1'){
        return $count . ' View';
        }
        //In all other cases return (count) Views
        else {
        return $count . ' Views';
        }
    }
}
?>

จากนั้นให้นำโค๊ดด้านล่างนี้ไปใส่ใว้ในไฟล์ single.php (หน้าแสดงบทความ) เพื่อที่จะทำการอัพเดทจำนวน views และแสดงจำนวน views
<?php if(function_exists('bac_PostViews')) {
    echo bac_PostViews(get_the_ID());
}?>
1
2
3
   
<?php if(function_exists('bac_PostViews')) {
    echo bac_PostViews(get_the_ID());
}?>

สำหรับหน้ารวมบทความ (content.php หรือ index.php ) ให้ใช้โค๊ดด้านล่างนี้เพื่อแสดงจำนวน Views ของแต่ละบทความ (แสดงอย่างเดียว ไม่อัพเดทจำนวน Views)
<?php echo (int) get_post_meta(get_the_ID(), 'post_views_count', true) . ' View(s)'; ?>
1
   
<?php echo (int) get_post_meta(get_the_ID(), 'post_views_count', true) . ' View(s)'; ?>

หาต้องการแสดงคอร์ลัมน์ view ในส่วนของ Admin Dashboard ให้เพิ่มโค๊ดด้านล่างนี้เข้าไปในไฟล์ function.php

admincountview
<?php
/*********************************CODE-3********************************************
* @Author: Boutros AbiChedid
* @Date:   January 16, 2012
* @Websites: http://bacsoftwareconsulting.com/ ; http://blueoliveonline.com/
* @Description: Adds a Non-Sortable 'Views' Columnn to the Post Tab in WP dashboard.
* This code requires CODE-1(and CODE-2) as a prerequesite.
* Code is browser and JavaScript independent.
* @Tested on: WordPress version 3.2.1
***********************************************************************************/
 
//Gets the  number of Post Views to be used later.
function get_PostViews($post_ID){
    $count_key = 'post_views_count';
    //Returns values of the custom field with the specified key from the specified post.
    $count = get_post_meta($post_ID, $count_key, true);
 
    return $count;
}
 
//Function that Adds a 'Views' Column to your Posts tab in WordPress Dashboard.
function post_column_views($newcolumn){
    //Retrieves the translated string, if translation exists, and assign it to the 'default' array.
    $newcolumn['post_views'] = __('Views');
    return $newcolumn;
}
 
//Function that Populates the 'Views' Column with the number of views count.
function post_custom_column_views($column_name, $id){
     
    if($column_name === 'post_views'){
        // Display the Post View Count of the current post.
        // get_the_ID() - Returns the numeric ID of the current post.
        echo get_PostViews(get_the_ID());
    }
}
//Hooks a function to a specific filter action.
//applied to the list of columns to print on the manage posts screen.
add_filter('manage_posts_columns', 'post_column_views');
 
//Hooks a function to a specific action.
//allows you to add custom columns to the list post/custom post type pages.
//'10' default: specify the function's priority.
//and '2' is the number of the functions' arguments.
add_action('manage_posts_custom_column', 'post_custom_column_views',10,2);
?>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
   
<?php
/*********************************CODE-3********************************************
* @Author: Boutros AbiChedid
* @Date:   January 16, 2012
* @Websites: http://bacsoftwareconsulting.com/ ; http://blueoliveonline.com/
* @Description: Adds a Non-Sortable 'Views' Columnn to the Post Tab in WP dashboard.
* This code requires CODE-1(and CODE-2) as a prerequesite.
* Code is browser and JavaScript independent.
* @Tested on: WordPress version 3.2.1
***********************************************************************************/
 
//Gets the  number of Post Views to be used later.
function get_PostViews($post_ID){
    $count_key = 'post_views_count';
    //Returns values of the custom field with the specified key from the specified post.
    $count = get_post_meta($post_ID, $count_key, true);
 
    return $count;
}
 
//Function that Adds a 'Views' Column to your Posts tab in WordPress Dashboard.
function post_column_views($newcolumn){
    //Retrieves the translated string, if translation exists, and assign it to the 'default' array.
    $newcolumn['post_views'] = __('Views');
    return $newcolumn;
}
 
//Function that Populates the 'Views' Column with the number of views count.
function post_custom_column_views($column_name, $id){
     
    if($column_name === 'post_views'){
        // Display the Post View Count of the current post.
        // get_the_ID() - Returns the numeric ID of the current post.
        echo get_PostViews(get_the_ID());
    }
}
//Hooks a function to a specific filter action.
//applied to the list of columns to print on the manage posts screen.
add_filter('manage_posts_columns', 'post_column_views');
 
//Hooks a function to a specific action.
//allows you to add custom columns to the list post/custom post type pages.
//'10' default: specify the function's priority.
//and '2' is the number of the functions' arguments.
add_action('manage_posts_custom_column', 'post_custom_column_views',10,2);
?>

 

ดูเพิ่มเติม : http://bacsoftwareconsulting.c...in-wordpress-without-a-plugin/
บันทึกการเข้า
หน้า: [1]   ขึ้นบน
พิมพ์