如何在wordpress中按日期的数量来改变帖子的顺序(How to change the order of posts by number of views not by date in wordp

编程入门 行业动态 更新时间:2024-10-27 22:21:31
如何在wordpress中按日期的数量来改变帖子的顺序(How to change the order of posts by number of views not by date in wordpress)

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

现在我想让我使用的主题来改变显示的帖子的顺序,以通过视图数量或评论而不是日期顺序来显示

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

I'm making a drawing website using WordPress and allowing my users to draw and post their drawings as pinned posts

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.

最满意答案

如果您想更改显示顺序,则可以使用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');


根据查看 次数评论次数来定购帖子

如果你想按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文件中使用。 代码已经过测试和工作。

希望这可以帮助!

If you want to changer the display order then you can use pre_get_posts action.

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

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. 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). Or also in any plugin php files. Code is tested and works.

Hope this helps!

更多推荐

本文发布于:2023-07-07 04:21:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1058914.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:顺序   数量   按日   帖子   如何在

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!