【问题标题】:How to sort posts by views, comments, rating etc wordpress如何按视图、评论、评分等 wordpress 对帖子进行排序
【发布时间】:2010-07-25 08:36:33
【问题描述】:

各位,我需要一种按浏览量、评分、cmets 对我的帖子进行排序的方法。我搜索了大量的插件,但它们都是错误的。

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

【问题讨论】:

  • 编写自己的CMS;这种方式更灵活、更强大。

标签: wordpress sorting


【解决方案1】:

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

至于功能 - wordpress 已经在帖子中存储了 cmets 的数量 - 但您需要获取它来存储页面视图/评级。首先,wp-postviews 可以正常工作——我们只是想要一些东西来存储数据。它带有专门的功能,可以根据您可以使用的受欢迎程度来获取帖子,但如果您想要更大的灵活性,我在下面包含了按视图数量或 cmets 数量排序的功能:

按 cmets 排序:

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;
}

然后只需在&lt;ul&gt; 或&lt;ol&gt; 标签内包含这些函数:get_most_visited() 和get_most_commented()(带有帖子数量的可选参数 - 默认为 10)。 (我已经包含了如何检索 cmets/views 的数量以防你想使用它们 - 否则你可以删除它们)

这种方法为您提供了很大的灵活性来展示帖子。基本上 - 这允许您轻松地使用一些基本的 CSS 样式或一些涉及 jQuery 的更花哨的东西来设置列表的样式。

对于帖子评分,Post Star Ratings 之类的插件可能会起到存储评分的作用,然后您可以使用与上述类似的功能。

希望这会有所帮助!

【讨论】:

    【解决方案2】:

    您将不得不在 Wordpress 中编写自己的自定义查询。这涉及熟悉 PHP 和一些 Wordpress API。

    一个好的起点:http://codex.wordpress.org/Displaying_Posts_Using_a_Custom_Select_Query

    至于您想要排序的方式,您首先需要获取观看次数和评分。我的直觉说您可以将所有内容存储在自定义字段中——因此请熟悉 post_meta 表。以下是我的一些想法:

    查看次数最多:在您的 single.php 中,每次加载帖子时,请确保添加一个递增的自定义字段 - 在您的 ORDER BY 查询中使用此自定义字段。

    【讨论】:

      猜你喜欢
      • 2021-10-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多