如何按视图,评论,评分等方式对帖子进行排序 [英] How to sort posts by views, comments, rating etc wordpress

查看:113
本文介绍了如何按视图,评论,评分等方式对帖子进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一种按观看次数,评分和评论对我的帖子进行排序的方法.搜索了很多插件,但它们都是马车.

Guys im in need of a way to sort my posts by views, rating, comments. ive search loads of plugings but their all buggy.

我想要这样的东西. 分类http://img138.imageshack.us/img138/2577/sorting.png

i want something like this. sorting http://img138.imageshack.us/img138/2577/sorting.png

推荐答案

要分离不同的排序方式,您可以使用jQuery之类的方式来创建选项卡式区域,在每种方法中,您都调用一个不同的(php)函数来对帖子进行排序相应地,然后在您的function.php文件中定义这些php函数.

For separating the different ways of sorting you could use something like jQuery to create a tabbed area, within each you call a different (php) function to sort your posts accordingly and then define these php functions in your function.php file.

关于功能-WordPress已经存储了帖子中的评论数量-但您需要使它存储页面浏览量/等级.首先, wp-postviews 可以正常工作-我们只需要一些东西来存储数据.它带有专用功能,可以按受欢迎程度获取帖子,您可以使用它,但是如果您想获得更大的灵活性,我在下面添加了一些功能,这些功能按查看次数或评论数量进行排序:

As for the functions - wordpress already stores the number of comments on a post -but you'll need to get it store page view / ratings. For the first, wp-postviews will work fine - we just want something to store the data. It comes with dedicated functions to get posts by popularity which you could use, but in case you wanted greater flexibility I've included functions below which sort by number of views or number of comments:

用于按评论排序:

function get_most_commented($limit=10) {
    global $wpdb;

   $most_commented = $wpdb->get_results("SELECT comment_count, ID, post_title FROM $wpdb->posts WHERE post_type='post' AND post_status = 'publish' ORDER BY comment_count DESC LIMIT 0 , $limit");

    foreach ($most_commented as $post) {
        setup_postdata($post);
        $id = $post->ID;
        $post_title = $post->post_title;
        $count = $post->comment_count;
        $output .= '<li><a href="'. get_permalink($id).'">'.$post_title. '</a> </li>';
    }
    return $output;
}

用于按帖子浏览量排序

function get_most_visited($limit=10) {
    global $wpdb;

    $most_viewed = $wpdb->get_results("SELECT DISTINCT $wpdb->posts.*, (meta_value+0) AS views FROM $wpdb->posts LEFT JOIN $wpdb->postmeta ON $wpdb->postmeta.post_id = $wpdb->posts.ID WHERE post_type='post' AND post_date < '".current_time('mysql')."' AND post_status = 'publish' AND meta_key = 'views' AND post_password = '' ORDER BY views DESC LIMIT $limit");


    foreach ($most_viewed as $post) {
            $id = $post->ID;
            $post_views = intval($post->views);
            $post_title = get_the_title($post);
            $post_title = $post->post_title;
             $output .= '<li><a href="'. get_permalink($id).'">'.$post_title. '</a>
     }

    return $output;
}

然后仅在<ul><ol>标记内包括以下功能:get_most_visited()get_most_commented()(带有可选的帖子数量参数-默认为10). (我已经包括了如何检索注释/视图的数量,以防您想使用它们,否则可以将其删除)

Then just include those functions: get_most_visited() and get_most_commented() (with an optional argument of number of posts - default is 10) inside <ul> or <ol> tags. (I've included how to retrieve the number of comments/views in case you wanted to use them - otherwise you could remove them)

此方法为您提供了很多展示帖子的灵活性.基本上-这使您可以轻松地使用一些基本的CSS样式或一些涉及jQuery的更奇特的样式来对列表进行样式设置.

This method gives you a lot of flexibility in how to present the posts. Basically - this allows you easily to style the list with some basic CSS styling or something a bit fancier involving jQuery.

对于帖子评分,可以使用诸如Post Star Ratings之类的插件来存储评分,然后使用与上面类似的功能.

As for post ratings a plugin like Post Star Ratings might do the trick to store ratings, and then you could use a similar function to the above.

希望这会有所帮助!

这篇关于如何按视图,评论,评分等方式对帖子进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆