【问题标题】:How to change the order of posts by number of views not by date in wordpress如何在wordpress中按查看次数而不是按日期更改帖子的顺序
【发布时间】:2017-02-24 18:20:51
【问题描述】:

我正在使用 WordPress 制作一个绘图网站,并允许我的用户绘制并将他们的绘图作为固定帖子发布

现在我想制作我使用的主题来更改显示帖子的顺序,以按查看次数或 cmets 显示,而不是按日期顺序显示

在哪里可以找到 WordPress 主题中帖子显示顺序的代码。

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

    如果你想改变显示顺序,那么你可以使用 pre_get_posts 操作。

    评论数排序

    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');
    


    查看订购帖子

    默认情况下,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);
    

    现在我们已经为视图设置了逻辑,所以我们将做空它

    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');
    


    viewcomment count 排序帖子

    如果你想orderby 这两个分数那么我们必须再次应用一个小技巧,因为 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);
        //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');
    

    请注意:为评论添加额外的权重不是强制性的,但如果读者真的喜欢这篇文章,那么只有他们才会发表评论。

    代码进入您的活动子主题(或主题)的 function.php 文件中。或者也可以在任何插件 php 文件中。
    代码已经过测试并且可以工作。

    希望这会有所帮助!

    【讨论】:

    • 是否可以将浏览次数最多的帖子和最近的帖子结合起来?
    猜你喜欢
    • 2020-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-29
    • 2018-06-23
    • 1970-01-01
    • 2023-04-02
    相关资源
    最近更新 更多