简体   繁体   中英

Wordpress Custom loop with two types of posts with different amounts

I have a category names "notes", I need on every page of the loop to show up to 10 notes, and up to 4 regular posts.

They both should be ordered by date.

ie I posted 1 note, note A , 1 hour ago and another note, note b , 5 hours ago, I also posted to posts, post A and post B 2 hours ago. in my loop I want to see note A , post A , post B , note B .

I hope it's clear enough.

I'm trying to do this with two custom WP_Queries, but I have hard time with them because of the global $post .

Any help would do! Thanks

Well, if anyone is interested, this is how I did it:

<?php
function elis_sortbydate ($a, $b) {
$time_a = strtotime($a->post_date);
$time_b = strtotime($b->post_date);
if ($time_a == $time_b) return 0;
return ($time_a > $time_b) ? -1 : 1;
}
$query = array_merge(array('cat' => 67, 'posts_per_page=10'), $wp_query->query);
    $notes_query = new WP_Query($query);

    $query = array_merge(array('cat' => -67, 'posts_per_page=4'), $wp_query->query);
    $posts_query = new WP_Query($query);
    if ($notes_query->have_posts() and $posts_query->have_posts()) {
        $all_posts = array_merge($notes_query->posts, $posts_query->posts);
        usort($all_posts, "elis_sortbydate");
        foreach ($all_posts as $post) { ... }
            }

the resulting $all_posts array is sorted by date (from high to low)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM