رفتن به مطلب

نمایش تعداد بازدید پست های وردپرس در پیشخوان


miraziz

پست های پیشنهاد شده

سلام. دوستان در مطالب آموزشی دیدم که امکان مشاهده تعداد بازدید پست‌های وردپرس بدون افزونه در قالب سایت وجود دارد.ولی میخوستم بدونم میشه این کار را با پیشخوان هم انجام داد. یعنی در پیشخوان در قسمت «همه‌ی نوشته‌ها» تعداد بازدید هر پست را نمایش دهد. مثلا کاری که افزونه wp-statistic انجام می‌دهد

لینک به ارسال
در 12 دقیقه قبل، Ghasemi گفته است :

سلام ،بله میشه . چون  متای تعداد بازدیدها از قبل ساخته شده میتونید ازش استفاده کنید . هم میتونید هوک بزنید ، هم میتونید متا اضافه کنید.

ممنون از پاسخگویتان. من متاسفانه به php وارد نیستنم امکان دارد کدش را بهم معرفی کنین و اینکه کجا باید قرارش بدم؟

طبق آموزش یک سایت خارجی پیش رفتم.ولی متاسفانه امکان ذخیره فایل توابع را نمی داد و می گفت در فلان خط اشتباهی رخ داده است

لینک به ارسال

سلام، می‌تونید از افزونه wp post views استفاده کنید. هم تو پیشخوان و هم تو سایت امکان نمایش بازدید رو خواهید داشت.

لینک به ارسال

اسم متایی که این مقدارو ذخیره میکنه باید پیدا کنید و کد زیرو تغییر بدین و تو function.php  قرار بدین

add_action( 'manage_posts_custom_column', 'admin_post_data_row', 10, 2);
function admin_post_data_row($column_name, $post_id)
{
    switch($column_name){
        case 'title':       
            edit_post_link(get_post_title($post_id), '<p>', '</p>',$post_id);
            echo '<br />'.get_post_meta($post_id,'field_name',true);
            break;
        default:
            break;
    }
}

ب جای field_name اسم متای خودتون رو قرار بدین

لینک به ارسال
در 21 دقیقه قبل، s4d3ghi گفته است :

سلام، می‌تونید از افزونه wp post views استفاده کنید. هم تو پیشخوان و هم تو سایت امکان نمایش بازدید رو خواهید داشت.

بله در جریان هستم. متاسفانه افزونه ای که معرفی کردید و انواع مشابه‌اش فشار زیادی بر روی هاست میآرند

لینک به ارسال
در 20 دقیقه قبل، Ghasemi گفته است :

اسم متایی که این مقدارو ذخیره میکنه باید پیدا کنید و کد زیرو تغییر بدین و تو function.php  قرار بدین

ب جای field_name اسم متای خودتون رو قرار بدین

ببخشین دقیقا دنبال چه متایی باشم. چون اصلا نمی دونم از چه متایی صحبت می کنید. امکان داره که اصلا اون متا وجود نداشته باشه و از اول اضافه اش کنیم.

کدهای توابع قالب را ضمیمه کردم.

funch.txt

لینک به ارسال
در 2 ساعت قبل، mehran-be گفته است :

سلام و درود @miraziz

بازدیدهای مطالب شما رو چه کد یا افزونه ای شمارش میکنه؟

سلام آقا مهران.هیچ افزونه ای قبلا از wp-statics استفاده می کردم که به دلیل اینکه افزونه ی سنگینی است حذفش کردم. برای آمار از Google Analytics استفاده می کنم که متاسفانه در پیشخوان وردس میزان بازدید هر پست را نشون نمیده مگر از افزونه‌ای رابط استفاده کنم که اون افزونه‌ها هم سرعت لودشون خیلی کند است.

تولینک زیر یک آموزش جامع است ولی متاسفانه وقتی کدها را در function.php قرار میدهم به هنگام سیو خطا میده.

https://artisansweb.net/display-post-views-count-post-screen-wordpress/

 

لینک به ارسال
در 23 دقیقه قبل، miraziz گفته است :

سلام آقا مهران.هیچ افزونه ای قبلا از wp-statics استفاده می کردم که به دلیل اینکه افزونه ی سنگینی است حذفش کردم. برای آمار از Google Analytics استفاده می کنم که متاسفانه در پیشخوان وردس میزان بازدید هر پست را نشون نمیده مگر از افزونه‌ای رابط استفاده کنم که اون افزونه‌ها هم سرعت لودشون خیلی کند است.

تولینک زیر یک آموزش جامع است ولی متاسفانه وقتی کدها را در function.php قرار میدهم به هنگام سیو خطا میده.

https://artisansweb.net/display-post-views-count-post-screen-wordpress/

کد زیر رو به فایل functions.php قالب سایتون اضافه کنید.

Mehran_Post_Views_Counter::instance();

final class Mehran_Post_Views_Counter {
	
	public static function instance() {
		
		new self();
		
	}
	
	public function __construct() {
		
		add_filter('manage_posts_columns', array($this, 'add_post_views_column'));
		add_action('manage_posts_custom_column', array($this, 'set_post_views_column_value'), 5, 2);
		add_action('loop_start', array($this, 'count_views_in_single'));
		
	}
	
	public function add_post_views_column($posts_columns) {
		
		return array_merge($posts_columns, array('post_views' => 'بازدید'));
		
	}
	
	public function set_post_views_column_value($column_name, $post_id) {
		
		if($column_name == 'post_views')
		
			echo self::get_post_views($post_id);
		
	}
	
	public function count_views_in_single(&$query) {
		
		if($query->is_singular() && $query->is_main_query())
			
			self::set_post_views($query->post->ID);
		
	}
	
	public static function get_post_views($post_id) {
		
		$count_key = '_mpvc';
		$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 "بدون بازدید";
			
		}
		
		return $count . ' بازدید';
		
	}
	
	public static function set_post_views($post_id) {
	
		$count_key = '_mpvc';
		$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, '0');
			
		} else {
			
			update_post_meta($post_id, $count_key, ++$count);
			
		}
		
	}
	
}

 

لینک به ارسال
در 28 دقیقه قبل، mehran-be گفته است :

کد زیر رو به فایل functions.php قالب سایتون اضافه کنید.

 

خیلی ممنون. کاملا درست کار کرد. فقط این کد تنها در ستون پیشخوان «همه‌ی نوشته‌ها» نشون خواهد داد؟ مثلا در صفحه «ویرایش نوشته» هم نشون میده؟

لینک به ارسال
در 3 دقیقه قبل، miraziz گفته است :

خیلی ممنون. کاملا درست کار کرد. فقط این کد تنها در ستون پیشخوان «همه‌ی نوشته‌ها» نشون خواهد داد؟ مثلا در صفحه «ویرایش نوشته» هم نشون میده؟

آره فقط توی همه نوشته ها نشون میده.

بخش ویرایش نوشته ننوشتم براش.

لینک به ارسال
در ۱ ساعت قبل، mehran-be گفته است :

آره فقط توی همه نوشته ها نشون میده.

بخش ویرایش نوشته ننوشتم براش.

کد زیر که برای نمایش تعداد بازدید هر نوشته در قالب سایت است. برای کد شما هم کار خواهد کرد؟

<?php
          setPostViews(get_the_ID());
?>

 

لینک به ارسال
در 2 ساعت قبل، miraziz گفته است :

کد زیر که برای نمایش تعداد بازدید هر نوشته در قالب سایت است. برای کد شما هم کار خواهد کرد؟

برای نمایش بازدید توی حلقه های قالب باید از کد زیر استفاده کنید.

<?= Mehran_Post_Views_Counter::get_post_views(get_the_ID()) ?>

 

در 2 ساعت قبل، miraziz گفته است :

و در پیشخوان وردپرس امکانی است برای sort کردن. مثلا در تصویر زیر برای تاریخ. ولی برای بازدیدها این اتفاق نمی افتد. این مشکل از وردپرس من است؟

ستون تعداد بازدید رو sortable تعریف نکردم.

لینک به ارسال
  • 3 ماه بعد...
در در 9/17/2018 at 19:27، mehran-b گفته است :

برای نمایش بازدید توی حلقه های قالب باید از کد زیر استفاده کنید.


<?= Mehran_Post_Views_Counter::get_post_views(get_the_ID()) ?>

 

ستون تعداد بازدید رو sortable تعریف نکردم.

سلام‌ آقا مهران و دوستان. خوب هستین.

یک سوالی داشتم. میشه این کد مربوط به نمایش تعداد بازدید در پیشخوان را طوری تغییر داد که تنها برای ادمین اصلی نمایش دهد و در پیشخوان سایر نویسندگان و مشترکان قابل نمایش نباشد؟

لینک به ارسال
در ۱ ساعت قبل، miraziz گفته است :

سلام‌ آقا مهران و دوستان. خوب هستین.

یک سوالی داشتم. میشه این کد مربوط به نمایش تعداد بازدید در پیشخوان را طوری تغییر داد که تنها برای ادمین اصلی نمایش دهد و در پیشخوان سایر نویسندگان و مشترکان قابل نمایش نباشد؟

کد قبلی رو با کد زیر جایگزین کنید.

Mehran_Post_Views_Counter::instance();

final class Mehran_Post_Views_Counter {
	
	public static function instance() {
		
		new self();
		
	}
	
	public function __construct() {
		
		add_filter('manage_posts_columns', array($this, 'add_post_views_column'));
		add_action('manage_posts_custom_column', array($this, 'set_post_views_column_value'), 5, 2);
		add_action('loop_start', array($this, 'count_views_in_single'));
		
	}
	
	public function add_post_views_column($posts_columns) {
		
		if(current_user_can('administrator'))
			
			return array_merge($posts_columns, array('post_views' => 'بازدید'));
			
		else
			
			return $posts_columns;
		
	}
	
	public function set_post_views_column_value($column_name, $post_id) {
		
		if($column_name == 'post_views')
		
			echo self::get_post_views($post_id);
		
	}
	
	public function count_views_in_single(&$query) {
		
		if($query->is_singular() && $query->is_main_query())
			
			self::set_post_views($query->post->ID);
		
	}
	
	public static function get_post_views($post_id) {
		
		$count_key = '_mpvc';
		$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 "بدون بازدید";
			
		}
		
		return $count . ' بازدید';
		
	}
	
	public static function set_post_views($post_id) {
	
		$count_key = '_mpvc';
		$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, '0');
			
		} else {
			
			update_post_meta($post_id, $count_key, ++$count);
			
		}
		
	}
	
}

 

لینک به ارسال
در در 1/12/2019 at 21:52، mehran-b گفته است :

کد قبلی رو با کد زیر جایگزین کنید.

سلام. این کد را قرار دادم. تعداد بازدیدها را هم برای ادمین اصلی و هم دیگر نویسندگان برای همه‌ی نوشته ها 0 نشون میده.

فقط کدهایی که در توابع من برای نمایش تعداد بازدید نوشته است به صورت زیر است. یعنی متفاوت تر از کدهایی است که شما گذاشته بودین.

//آمار بازدید پست ها				
	
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 . ' بازدید';
     
    //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 . ' بازدید';
        }
        //In all other cases return (count) Views
        else {
        return $count . ' بازدیدها';
        }
    }
}		
			
		
		
//Function: 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: Add/Register the Non-sortable 'Views' Column to your Posts tab in WP Dashboard.
function post_column_views($newcolumn){
    //Retrieves the translated string, if translation exists, and assign it to the 'default' array.
    $newcolumn['post_views'] = __('بازدیدها');
    return $newcolumn;
}
//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' );
 
//Function: Populate the 'Views' Column with the views count in the WP dashboard.
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 action. 
//allows you to add custom columns to the list post/custom post type pages.
//'10' default: specify the function's priority.
//'2' is the number of the functions' arguments.
add_action('manage_posts_custom_column', 'post_custom_column_views',10,2);
 
/**UP TO HERE IS THE SAME AS CODE-3. NEXT IS WHAT IS REQUIRED FOR SORTABLE 'VIEWS' COLUMN**/
 
//Function: Register the 'Views' column as sortable in the WP dashboard.
function register_post_column_views_sortable( $newcolumn ) {
    $newcolumn['post_views'] = 'post_views';
    return $newcolumn;
}
add_filter( 'manage_edit-post_sortable_columns', 'register_post_column_views_sortable' );
 
//Function: Sort Post Views in WP dashboard based on the Number of Views (ASC or DESC).
function sort_views_column( $vars ) 
{
    if ( isset( $vars['orderby'] ) && 'post_views' == $vars['orderby'] ) {
        $vars = array_merge( $vars, array(
            'meta_key' => 'post_views_count', //Custom field key
            'orderby' => 'meta_value_num') //Custom field value (number)
        );
    }
    return $vars;
}
add_filter( 'request', 'sort_views_column' );		
?>		

 

لینک به ارسال
در 3 دقیقه قبل، miraziz گفته است :

سلام. این کد را قرار دادم. تعداد بازدیدها را هم برای ادمین اصلی و هم دیگر نویسندگان برای همه‌ی نوشته ها 0 نشون میده.

فقط کدهایی که در توابع من برای نمایش تعداد بازدید نوشته است به صورت زیر است. یعنی متفاوت تر از کدهایی است که شما گذاشته بودین.

یعنی هم برای ادمین اصلی و هم برای نویسنده ها ستون بازدید رو توی پیشخوان نمایش میداد همچنان؟

کاری به صفر شدن ندارم.

لینک به ارسال
در 21 ساعت قبل، mehran-b گفته است :

یعنی هم برای ادمین اصلی و هم برای نویسنده ها ستون بازدید رو توی پیشخوان نمایش میداد همچنان؟

کاری به صفر شدن ندارم.

بله. ستون نوشته را هم برای ادمین اصلی و هم نویسنده ها نشون می داد

لینک به ارسال
در 2 ساعت قبل، miraziz گفته است :

بله. ستون نوشته را هم برای ادمین اصلی و هم نویسنده ها نشون می داد

اینو تست کنید.

<?php

//آمار بازدید پست ها				
	
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 . ' بازدید';
     
    //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 . ' بازدید';
        }
        //In all other cases return (count) Views
        else {
        return $count . ' بازدیدها';
        }
    }
}		
			
		
		
//Function: 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: Add/Register the Non-sortable 'Views' Column to your Posts tab in WP Dashboard.
function post_column_views($newcolumn){
    //Retrieves the translated string, if translation exists, and assign it to the 'default' array.
	if(current_user_can('administrator')) {
		
		$newcolumn['post_views'] = __('بازدیدها');
		
		return $newcolumn;
		
	} else {
		
		return $newcolumn;
		
	}
	
}
//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' );
 
//Function: Populate the 'Views' Column with the views count in the WP dashboard.
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 action. 
//allows you to add custom columns to the list post/custom post type pages.
//'10' default: specify the function's priority.
//'2' is the number of the functions' arguments.
add_action('manage_posts_custom_column', 'post_custom_column_views',10,2);
 
/**UP TO HERE IS THE SAME AS CODE-3. NEXT IS WHAT IS REQUIRED FOR SORTABLE 'VIEWS' COLUMN**/
 
//Function: Register the 'Views' column as sortable in the WP dashboard.
function register_post_column_views_sortable( $newcolumn ) {
	
	if(current_user_can('administrator')) {
		
		$newcolumn['post_views'] = __('بازدیدها');
		
		return $newcolumn;
		
	} else {
		
		return $newcolumn;
		
	}
	
}
add_filter( 'manage_edit-post_sortable_columns', 'register_post_column_views_sortable' );
 
//Function: Sort Post Views in WP dashboard based on the Number of Views (ASC or DESC).
function sort_views_column( $vars ) 
{
    if ( isset( $vars['orderby'] ) && 'post_views' == $vars['orderby'] ) {
        $vars = array_merge( $vars, array(
            'meta_key' => 'post_views_count', //Custom field key
            'orderby' => 'meta_value_num') //Custom field value (number)
        );
    }
    return $vars;
}
add_filter( 'request', 'sort_views_column' );		
?>

 

لینک به ارسال
در 14 ساعت قبل، mehran-b گفته است :

خواهش میکنم.

موفق باشید

آقا مهران این امکان مخفی کردن را می توان فقط در مورد ستون ها پیاده کرد؟ یا نه، برای موارد دیگر، مثلا «نوشته‌های در انتظار برررسی» هم می‌توان همین کار را کرد؟

لینک به ارسال

به گفتگو بپیوندید

هم اکنون می توانید مطلب خود را ارسال نمایید و بعداً ثبت نام کنید. اگر حساب کاربری دارید، برای ارسال با حساب کاربری خود اکنون وارد شوید .

مهمان
ارسال پاسخ به این موضوع ...

×   شما در حال چسباندن محتوایی با قالب بندی هستید.   حذف قالب بندی

  تنها استفاده از 75 اموجی مجاز می باشد.

×   لینک شما به صورت اتوماتیک جای گذاری شد.   نمایش به صورت لینک

×   محتوای قبلی شما بازگردانی شد.   پاک کردن محتوای ویرایشگر

×   شما مستقیما نمی توانید تصویر خود را قرار دهید. یا آن را اینجا بارگذاری کنید یا از یک URL قرار دهید.

×
×
  • اضافه کردن...