简体   繁体   English

如何在Wordpress中按视图数而不是日期更改帖子顺序

[英]How to change the order of posts by number of views not by date in wordpress

I'm making a drawing website using WordPress and allowing my users to draw and post their drawings as pinned posts 我正在使用WordPress创建一个绘图网站,并允许我的用户以固定帖子的形式绘制和发布其图形

在此处输入图片说明在此处输入图片说明

Now I want to make the theme I'm using to change the order of the displayed posts to be displayed by number of views or comments not by the date order 现在,我要使我正在使用的主题更改显示的帖子的顺序,使其按视图或评论数而不是按日期顺序显示

Where can I find the code responsible for the posts displaying order in the theme of WordPress. 在哪里可以找到负责以WordPress为主题显示帖子的代码。

If you want to changer the display order then you can use pre_get_posts action. 如果要更改显示顺序,则可以使用pre_get_posts操作。

To order post by comment count 评论数订购帖子

function wh_post_display_order_comment($query) {
    if ($query->is_home() && $query->is_main_query()) {
        $query->set('orderby', 'comment_count');
        $query->set('order', 'DESC');
    }
}

add_action('pre_get_posts', 'wh_post_display_order_comment');


To order post by view 视图订购帖子

By default WordPress does not have any option to short post by view so you have to use a little trick 默认情况下,WordPress没有选择按视图简短发布的选项,因此您必须使用一些技巧

function whpp_track_post_views($post_id) {
    if (!is_single())
        return;
    if (empty($post_id)) {
        global $post;
        $post_id = $post->ID;
    }
    whpp_set_post_views($post_id);
}

add_action('wp_head', 'whpp_track_post_views');

function whpp_set_post_views($postID) {
    $count_key = 'whpp_track_post_views';
    $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);
    }
}

//To keep the count accurate, lets get rid of prefetching
remove_action('wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0);

Now that we have set our logic for view so we 'll be shorting it 现在我们已经设置了视图逻辑,因此我们将其简化

function wh_post_display_order_view($query) {
    if ($query->is_home() && $query->is_main_query()) {
        $query->set('meta_key', 'whpp_track_post_views');
        $query->set('orderby', 'meta_value_num');
        $query->set('order', 'DESC');
    }
}

add_action('pre_get_posts', 'wh_post_display_order_view');


To order post by both view and comment count 通过查看评论计数订购帖子

If you want to orderby both the score then again we have to apply a small trick, as there is no default option in WordPress. 如果您想按两个分数orderby ,那么我们必须再次应用一个小技巧,因为WordPress中没有默认选项。 First we'll count the comment and we'll add a small weightage and add the total view count and keep it is a different meta field and then we'll sort the post on that key. 首先,我们将对评论进行计数,并增加一小部分权重,并增加总观看次数,并将其保留为其他元字段,然后对该键上的帖子进行排序。

function whpp_track_post_views($post_id) {
    if (!is_single())
        return;
    if (empty($post_id)) {
        global $post;
        $post_id = $post->ID;
    }
    whpp_set_post_views($post_id);
}

add_action('wp_head', 'whpp_track_post_views');

function whpp_set_post_views($postID) {
    $count_key = 'whpp_track_post_views';
    $count = get_post_meta($postID, $count_key, true);
    //retriving total comments
    $comments_count = wp_count_comments($postID);
    $total_comment = $comments_count->total_comments;

    $comment_point = 2; //change the number with your desired weightage
    $comment_score = $total_comment * $comment_point;
    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);
    }
    update_post_meta($postID, 'whpp_view_comment_score', ($count + $comment_score));
}

//To keep the count accurate, lets get rid of prefetching
remove_action('wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0);


function wh_post_display_order_view($query) {
    if ($query->is_home() && $query->is_main_query()) {
        $query->set('meta_key', 'whpp_view_comment_score');
        $query->set('orderby', 'meta_value_num');
        $query->set('order', 'DESC');
    }
}

add_action('pre_get_posts', 'wh_post_display_order_view');

Please Note : Adding extra weight to comment is not mandatory, but if reader will realy like the post then only they 'll give the comment. 请注意: 添加评论不是强制性的,但是如果读者真的很喜欢这篇文章,那么只有他们发表评论。

Code goes in function.php file of your active child theme (or theme). 代码进入您的活动子主题(或主题)的function.php文件中。 Or also in any plugin php files. 或在任何插件php文件中。
Code is tested and works. 代码已经过测试并且可以正常工作。

Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM