这是一个开始(找到函数here);
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 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);
}
}
// Remove issues with prefetching adding extra views
remove_action( \'wp_head\', \'adjacent_posts_rel_link_wp_head\', 10, 0);
现在将此添加到您的单曲中。用于跟踪视图的php文件;
<?php setPostViews(get_the_ID()); ?>
现在可以使用元字段
post_views_count
创建一个简单的WP\\U查询循环,显示视图数最多的10篇文章。Eg;
$args = array(
\'posts_per_page\' => 10,
\'meta_key\' => \'post_views_count\',
\'orderby\' => \'meta_value_num\',
\'order\' => \'DESC\'
);
$top_posts = new WP_Query($args);
while ($top_posts->have_posts()) : $top_posts->the_post();
// Your post loop content
endwhile;
wp_reset_postdata();