【发布时间】:2017-02-24 18:20:51
【问题描述】:
我正在使用 WordPress 制作一个绘图网站,并允许我的用户绘制并将他们的绘图作为固定帖子发布
现在我想制作我使用的主题来更改显示帖子的顺序,以按查看次数或 cmets 显示,而不是按日期顺序显示
在哪里可以找到 WordPress 主题中帖子显示顺序的代码。
【问题讨论】:
我正在使用 WordPress 制作一个绘图网站,并允许我的用户绘制并将他们的绘图作为固定帖子发布
现在我想制作我使用的主题来更改显示帖子的顺序,以按查看次数或 cmets 显示,而不是按日期顺序显示
在哪里可以找到 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 文件中。
代码已经过测试并且可以工作。
希望这会有所帮助!
【讨论】: