1

How can one split a loop via WP_Query into two columns without having to work with two loops and it is possible that for example in the left side those posts of category "A and B" and in the right side those posts of category "C and D ". The problem is that one can theoretically create two different loops with different parameters for the query, but the page navigation only takes the results of one loop into account (since a page navigation should work for both columns). Thanks

enter image description here

1
  • what is your query? please post the full code of the loop of your WP_Query
    – Michael
    Jan 6, 2017 at 6:05

2 Answers 2

0

How can one split a loop via WP_Query into two columns without having to work with two loops

These would be possible with if conditional.

and it is possible that for example in the left side those posts of category "A and B" and in the right side those posts of category "C and D ".

Yes. Sure.

The problem is that one can theoretically create two different loops with different parameters for the query, but the page navigation only takes the results of one loop into account (since a page navigation should work for both columns).

You can also get two queries, one by one and merge the results. Just an option.

$enough = 20;
$args = array(
    'posts_per_page'    => $enough,
    'post_type'         => array('post');
    'category__in'      => array('A','B','C','D'),
    ... // some more 
);

// query
$q = new WP_Query( $args );
while( $q->have_posts() ) : $q->the_post();
global $post;
if(in_category(array('A', 'B') , $post)) {      
?>
<div class="left">
<?php
    the_title('<title>','</title>',true);
    the_content(); ?>
</div>
}else{ ?>

<div class="right">
<?php
    the_title('<title>','</title>',true);
    the_content(); ?>
</div>
<?php
}

... // block
endwhile;

If you prefer you can use CSS column-count: 2 and two queries that would give you 10 results each.

BTW, I took a constraint that no articles can have at the same multiple categories.

0

If i was doing it i would say:

<?php 
// args  
$args = array(
    'posts_per_page'    => -1,
    'post_type'     => 'post',
);
// query
$the_query = new WP_Query( $args );

 if( $the_query->have_posts() ):
    $column1 ="";
    $column2 ="";
    while( $the_query->have_posts() ) : $the_query->the_post();
     if ($critera='TRUE') {
       $column1 =. '<div>'.get_title().'</div>'; //your own code here
     }
     if ($critera='FALSE') {
       $column2 =. '<div>'.get_title().'</div>'; //your own code here
     }
    echo '<div class="row">';
    echo '<div class="column left">'.$column1.'</div>';
    echo '<div class="column right">'.$column2.'</div>';
    echo '</div>';  //ends row
    php endwhile;
endif;
wp_reset_query();    // Restore global post data stomped by the_post(). ?>  

This is just a sample though, because I'm not sure what the condition you are using to decide in which column each post will be and you didn't post your code. You'll need to make sure your code is in the conditionals as well and whatever theme you are using has the rows and columns (class names etc). The idea is there though and this definitely works for me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.