Wrapping every 3 elements in a loop leaves an empty wrapper

前端 未结 1 652
执念已碎
执念已碎 2021-01-25 02:15

I am wrapping every 3 elements in my loop in a wrapper div like this:

$query = array(
    \'post_type\' => \'post\',
);

$i = 1;

$posts = new WP_Query( $quer         


        
1条回答
  •  -上瘾入骨i
    2021-01-25 02:47

    Add the wrapper inside:

    $query = array(
        'post_type' => 'post',
    );
    
    $i = 1;
    
    $posts = new WP_Query( $query );
    $out = '';
    $endingNeeded = false;
    if ($posts->have_posts()){
        while ($posts->have_posts()){
    
            if($i % 3 == 1) {
                $out .= '
    '; $endingNeeded = true; } $posts->the_post(); $out.= '
    //content here
    '; if($i % 3 == 0) { $out .= '
    '; $endingNeeded = false; } $i++; } } if($endingNeeded) { $out .= '
    '; } wp_reset_postdata(); return '
    '.$out.'
    ';

0 讨论(0)
提交回复
热议问题