dtrovwl75780 2015-01-04 20:37
浏览 56
已采纳

WordPress:显示所有类别的帖子,每个类别最多5个帖子,并按日期排序?

I am not sure if this has been answered before, but vigorous googling has not led me anywhere so far.

I have a wordpress site where I would like to display all posts as usual, ordered by date. However, instead of setting a limit on the total number of posts displayed, I would like to set a limit on each category.

For instance, if I have two categories FOO and BAR, I want WordPress to display all, but at most 5, posts from both FOO and BAR. The posts should still be ordered by date, and I mean that any post from FOO that was posted before one from BAR appears first and vice versa.

Specific problem: There is one post in the category FOO from last year, then 20 entries in BAR since then and now I am adding another post in the category FOO. Usually, Wordpress with a post limit of 10 would display this most recent FOO entry and 9 more BAR entries. However, I would like it to display both my FOO entries and the 5 most recent BAR entries. Only after I add 4 more FOO entries, the FOO entry from last year will no longer be displayed.

What is the best, most clean and maintainable way to achieve this?

I would be very grateful for any help.

  • 写回答

3条回答 默认 最新

  • duangan2307 2015-01-04 21:51
    关注

    Given that there seems no other solution that doing an insane amount of queries, I devised the following, added to functions.php.

    function alter_query( $query ) {
        if ( $query->is_home() && $query->is_main_query() ) {
            $post_limit = floor(get_settings('posts_per_page')/2);      
            $the_posts = array();
            foreach (get_terms('category',array('hide_empty'=>1,'fields'=>'ids')) as $id) 
                $the_posts = array_merge( $the_posts, 
                    get_posts( array(
                        'fields'      => 'ids',
                        'numberposts' => $post_limit,
                        'category'    => $id, 
                        'orderby'     => 'post_date')));
            $query->set('post__in',    $the_posts);
            $query->set('numberposts', -1);
            $query->set('orderby',     'post_date');
        }
    }
    add_action( 'pre_get_posts', 'alter_query' );
    

    In this solution, I only fetch the ids of all relevant posts first, instead of all the contents and then perform a query to retrieve the actual data. I prefer this solution so far because I can use it without even touching my main loop, which is modified via the hook pre_get_posts.

    I would still prefer a solution that is a little faster, i.e. one that only performs a single query.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决