محمد اصفار نشر 10 مايو 2022 أرسل تقرير مشاركة نشر 10 مايو 2022 (معدل) السلام عليكم استخدم هذه الدالة لتخزين عدد قراء الصفحة ، هل يمكن جعلها تخزن بشكل منفصل عدد القراء لليوم والاسبوع function wpb_set_post_views($postID) { $count_key = 'wpb_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); } } remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0); function wpb_get_post_views($postID){ $count_key = 'wpb_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 "0 views"; } return $count.' views'; } تم التعديل في 17 مايو 2022 بواسطة Hassan Hedr توضيح العنوان اقتباس رابط هذا التعليق شارك على الشبكات الإجتماعية More sharing options...
1 Adnane Kadri نشر 10 مايو 2022 أرسل تقرير مشاركة نشر 10 مايو 2022 هل تستعمل وردبرس؟ اقتباس رابط هذا التعليق شارك على الشبكات الإجتماعية More sharing options...
0 محمد اصفار نشر 10 مايو 2022 الكاتب أرسل تقرير مشاركة نشر 10 مايو 2022 نعم وردبريس اقتباس رابط هذا التعليق شارك على الشبكات الإجتماعية More sharing options...
0 Hassan Hedr نشر 17 مايو 2022 أرسل تقرير مشاركة نشر 17 مايو 2022 يمكنك إضافة بيانات وصفية meta values إضافية خاصة باليوم والأسبوع الحالي، عبر إضافة بادئة لاسم المفتاح تدل على اليوم أو الأسبوع الحالي باستخدام التابع date والتنسيقات التالية: Y يدل على السنة الحالية m يدل عل الشهر الحالي d يدل على تاريخ اليوم W تدل على الأسبوع الحالي ضمن السنة وتعدل عليها دومًا كالتالي: function wpb_set_post_views($postID) { function increment_view_count($postID, $key) { $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); } } // الكلي increment_view_count($postID, 'wpb_post_views_count'); // يومي increment_view_count($postID, date('Y_m_d') . '_wpb_post_views_count'); // 2022_05_17_wpb_post_views_count // أسبوعي increment_view_count($postID, date('Y_W') . '_wpb_post_views_count'); // 2022_20_wpb_post_views_count } لاحظ استخراج منطق جلب تعيين المشاهدات لأنه متكرر، وبنفس الطريقة نجلب نفس المفاتيح لليوم الحالي، لاحظ أيضًا استخراج منطق جلب عدد المشاهدات إلى تابع منفصل لأن العملية مكررة، يمكنك تنسيق النتيجة كما هو مناسب لتطبيقك كالتالي: function wpb_get_post_views($postID) { function get_view_count($postID, $key) { $count = get_post_meta($postID, $key, true); if ($count == '') { delete_post_meta($postID, $key); add_post_meta($postID, $key, '0'); return 0; } return $count; } return sprintf( '%d total views - %d views weekly - %d views today', // الكلي get_view_count($postID, 'wpb_post_views_count'), // يومي get_view_count(date($postID, 'Y_m_d') . '_wpb_post_views_count'), // أسبوعي get_view_count($postID, date('Y_W') . '_wpb_post_views_count'), ); } يمكنك الاستفادة من قراءة المقال التالي: اقتباس رابط هذا التعليق شارك على الشبكات الإجتماعية More sharing options...
السؤال
محمد اصفار
السلام عليكم
استخدم هذه الدالة لتخزين عدد قراء الصفحة ، هل يمكن جعلها تخزن بشكل منفصل عدد القراء لليوم والاسبوع
function wpb_set_post_views($postID) { $count_key = 'wpb_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); } } remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0); function wpb_get_post_views($postID){ $count_key = 'wpb_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 "0 views"; } return $count.' views'; }
توضيح العنوان
رابط هذا التعليق
شارك على الشبكات الإجتماعية
3 أجوبة على هذا السؤال
Recommended Posts
انضم إلى النقاش
يمكنك أن تنشر الآن وتسجل لاحقًا. إذا كان لديك حساب، فسجل الدخول الآن لتنشر باسم حسابك.