wordpress如何基于当日浏览增量来排序文章而不是基于所有浏览量,那么我们这里需要记录每天的浏览量。
首先需要安装一个插件,wordpress popular posts。
然后在主题的functions.php里加上以下代码:
function custom_wpp_update_postviews($postid) { // Accuracy: // 10 = 1 in 10 visits will update view count. (Recommended for high traffic sites.) // 30 = 30% of visits. (Medium traffic websites.) // 100 = Every visit. Creates many db write operations every request. $accuracy = 50; if ( function_exists('wpp_get_views') && (mt_rand(0,100) < $accuracy) ) { // Remove or comment out lines that you won't be using!! update_post_meta( $postid, 'views_total', wpp_get_views($postid, 'all', false) ); update_post_meta( $postid, 'views_daily', wpp_get_views($postid, 'daily', false) ); update_post_meta( $postid, 'views_weekly', wpp_get_views($postid, 'weekly', false) ); update_post_meta( $postid, 'views_monthly', wpp_get_views($postid, 'monthly', false) ); } } add_action('wpp_post_update_views', 'custom_wpp_update_postviews');
然后在需要显示热门文章的地方这样调用:
$args = array( 'post_type' => 'post', 'meta_key' => 'views_weekly', 'orderby' => 'meta_value_num', 'order' => 'DESC', 'posts_per_page' => '3', ); $top_posts = new WP_Query($args);
如果发现加上以上代码还是不生效,那是因为需要重新计算浏览量,你需要把上面的$accuracy的值改成100%即可。You only need to do this step once. When you are done, change the $accuracy
again.
0 个评论