كود المشاهدات
افتح ملف functions.php
ضع نهايته الكود التالي
لا تنسى تعريب كلمة مشاهدة Views و View
\[code\]<?php
// function to display number of posts.
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, '0');
return "0 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 = 0;
delete\_post\_meta($postID, $count\_key);
add\_post\_meta($postID, $count\_key, '0');
}else{
$count++;
update\_post\_meta($postID, $count\_key, $count);
}
}
// Add it to a column in WP-Admin
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());
}
}
?>\[/code\]
افتح ملف single.php
وضع بدايته الكود التالي
\[code\]<?php setPostViews(get\_the\_ID()); ?>\[/code\]
----
اخيرا ضع او استبدل الكود التالي في المكان الذي تريد ان تظهر فيه المشاهدات
علما ان الكود يعمل كعداد لعدد مرات فتح التدوينة وليس حساب الزوار بشكل دقيق
\[code\]<?php echo getPostViews(get\_the\_ID()); ?>\[/code\]
هذا الكود استبدله بالكود الذي يلي كلمة الزيارات في الملفات
single.php - index.php - archive.php - category.php - search.php
المصدر
http://wp-snippets.com/post-views-without-plugin/