最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

wp query - Multiple wordpress queries with nested output (odd - even)

programmeradmin2浏览0评论

I need to show the output of two wordpress queries like below.

output example:

query 1 - post 1
query 2 - post 1
query 1 - post 2
query 2 - post 2
query 1 - post 3
query 2 - post 3
...........

mycode:

<?php
$args_one = array(
        'cat'              => 7,
        'posts_per_page'   => 8
    );
$args_two = array(
        'cat'              => 10,
        'posts_per_page'   => 8
    ); 

$the_query_one = new WP_Query( $args_one );

if ( $the_query_one->have_posts() ) :
        $num = 1;
        while ( $the_query_one->have_posts() ) : $the_query_one->the_post();
                if ($num % 2 == 0) {
                        $even   .= get_template_part('mobile-template/loop');
                } else {
                        $odd    .= get_template_part('mobile-template/loop');
                }
        $num++;        
        endwhile;
        wp_reset_postdata();

endif;

$the_query_two = new WP_Query( $args_two );

if ( $the_query_two->have_posts() ) :
        $num = 1;
        while ( $the_query_two->have_posts() ) : $the_query_two->the_post();
                if ($num % 2 == 0) {
                        $even   .= get_template_part('mobile-template/loop');
                } else {
                        $odd    .= get_template_part('mobile-template/loop');
                }
        $num++;   
        endwhile;
        wp_reset_postdata();

endif; 
      echo $even . $odd;
?>

This code works fine but I need a better or WP Standard way.

I need to show the output of two wordpress queries like below.

output example:

query 1 - post 1
query 2 - post 1
query 1 - post 2
query 2 - post 2
query 1 - post 3
query 2 - post 3
...........

mycode:

<?php
$args_one = array(
        'cat'              => 7,
        'posts_per_page'   => 8
    );
$args_two = array(
        'cat'              => 10,
        'posts_per_page'   => 8
    ); 

$the_query_one = new WP_Query( $args_one );

if ( $the_query_one->have_posts() ) :
        $num = 1;
        while ( $the_query_one->have_posts() ) : $the_query_one->the_post();
                if ($num % 2 == 0) {
                        $even   .= get_template_part('mobile-template/loop');
                } else {
                        $odd    .= get_template_part('mobile-template/loop');
                }
        $num++;        
        endwhile;
        wp_reset_postdata();

endif;

$the_query_two = new WP_Query( $args_two );

if ( $the_query_two->have_posts() ) :
        $num = 1;
        while ( $the_query_two->have_posts() ) : $the_query_two->the_post();
                if ($num % 2 == 0) {
                        $even   .= get_template_part('mobile-template/loop');
                } else {
                        $odd    .= get_template_part('mobile-template/loop');
                }
        $num++;   
        endwhile;
        wp_reset_postdata();

endif; 
      echo $even . $odd;
?>

This code works fine but I need a better or WP Standard way.

Share Improve this question asked Mar 13, 2020 at 21:33 shahriyar.mshahriyar.m 133 bronze badges 3
  • That doesn't look right to me? You'll get all Q1 evens, Q2 evens, Q1 odds then Q2 odds, not the interleaving you want. – Rup Commented Mar 13, 2020 at 22:01
  • With some complex SQL you could do this with a single query. Depending on how the MySQL driver works (I'm not sure), if you can run two queries in parallel then you could have both WP_Queries open at once and call the_post on each one in turn. But if you are going to / do need to render each query in turn I'd put the generated strings into an array or arrays and read them out in the right order rather than concatenating everything into even / odd strings like this. – Rup Commented Mar 13, 2020 at 22:09
  • Yes my code does not get the combination I want. Maybe I need to make a array first and then create a foreach loop? – shahriyar.m Commented Mar 13, 2020 at 22:17
Add a comment  | 

1 Answer 1

Reset to default 1

Why not just do straight what you've wanted.

<?php
$args_one = array(
    'cat'            => 7,
    'posts_per_page' => 8,
);
$args_two = array(
    'cat'            => 10,
    'posts_per_page' => 8,
);

// will run 2 sql queries.
$posts_one = get_posts( $args_one );
$posts_two = get_posts( $args_two );

$all_posts = array();

// lets merge them into 1 array.
while ( isset( $posts_one[0] ) || isset( $posts_two[0] ) ) {

    if ( isset( $posts_one[0] ) ) {
        $all_posts[] = array_shift( $posts_one );
    }

    if ( isset( $posts_two[0] ) ) {
        $all_posts[] = array_shift( $posts_two );
    }
}
/**
 * Now we've merged them gracefully into 1 array regarless of if these arrays are equal in size or not.
 * so we can now render them in single loop.
 */

if ( count( $all_posts ) ) :
    $num = 1;
    global  $post;
    foreach ( $all_posts as $post ) :
        setup_postdata($post);
        if ( 0 === $num % 2 ) {
            get_template_part( 'mobile-template/loop-even' ); // i've added suffix -even|-odd to make it possible to render them a bit differently.
        } else {
            get_template_part( 'mobile-template/loop-odd' );
        }
        $num++;
    endforeach;
    wp_reset_postdata();

endif;
发布评论

评论列表(0)

  1. 暂无评论