Php 如何在wordpress中按视图数量而不是按日期更改帖子顺序
我正在使用WordPress制作一个绘图网站,允许我的用户绘制并将他们的绘图作为固定的帖子发布 现在,我想让我用来更改显示文章顺序的主题按视图数或评论数显示,而不是按日期顺序显示 我在哪里可以找到负责WordPress主题中帖子显示顺序的代码 如果要更改显示顺序,则可以使用 行动 按评论计数订购帖子Php 如何在wordpress中按视图数量而不是按日期更改帖子顺序,php,wordpress,Php,Wordpress,我正在使用WordPress制作一个绘图网站,允许我的用户绘制并将他们的绘图作为固定的帖子发布 现在,我想让我用来更改显示文章顺序的主题按视图数或评论数显示,而不是按日期顺序显示 我在哪里可以找到负责WordPress主题中帖子显示顺序的代码 如果要更改显示顺序,则可以使用 行动 按评论计数订购帖子 按视图订购邮件 默认情况下,WordPress没有任何选项可以按视图短发帖子,因此您必须使用一些小技巧 function whpp_track_post_views($post_id) {
按视图订购邮件 默认情况下,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);
现在我们已经为view设置了逻辑,所以我们将缩短它
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');
按查看和评论计数排序帖子 如果你想同时获得分数,那么我们必须应用一个小技巧,因为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文件中使用。代码经过测试并正常工作。
希望这有帮助 有没有可能使它成为浏览次数最多的帖子和最近发布的帖子的组合?
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');