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 springboot 3.0 实现Security 6.x版本集成
  • ¥15 PHP-8.1 镜像无法用dockerfile里的CMD命令启动 只能进入容器启动,如何解决?(操作系统-ubuntu)
  • ¥15 请帮我解决一下下面六个代码
  • ¥15 关于资源监视工具的e-care有知道的嘛
  • ¥35 MIMO天线稀疏阵列排布问题
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)