Repeating the rows within Loop

痴心易碎 提交于 2020-01-14 06:37:08

问题


I'm working on a WordPress posts loop with same rows repeating. I achieved the first row but the 2nd row isn't looping whereas the first row loops perfectly.

Below is the code for the loop and the screenshot.

Loop Code

$count = 1;

$featured_posts = new \WP_Query( $args );
if ( $featured_posts->have_posts() ) : while ($featured_posts->have_posts()) : $featured_posts->the_post();
    if ( 1 == $count % 5 ) {
        echo '<div class="wh-tiles-posts-left">';
    } elseif ( 2 == $count % 5 ) {
        echo '<div class="wh-tiles-posts-right">';
    } elseif ( 4 == $count % 9 ) {
        echo '<div class="wh-tiles-posts-left2">';
    } elseif ( 5 == $count % 10 ) {
        echo '<div class="wh-tiles-posts-right2">';
    }
    $this->render_post_body( $count );

    if ( 1 == $count % 5  ) {
        echo '</div>';
    } elseif ( 3 == $count % 6 ) {
        echo '</div>';
    } elseif( 4 == $count % 9 ) {
        echo '</div>';
    } elseif ( 5 == $count % 10 ) {
        echo '</div>';
    }
    $count++;

    endwhile;
    endif;
    wp_reset_postdata();

What am I doing wrong?. The div with class names output shown in the image 2 below for better explanation.


回答1:


I think it's to do with your calculations as to when to add the various div tags, I've changed the calculations, especially only having 1 close test...

if ( 0 == ($count-1) % 5 ) {
    echo '<div class="wh-tiles-posts-left">';
} elseif ( 1 == ($count-1) % 5 ) {
    echo '<div class="wh-tiles-posts-right">';
} elseif ( 3 == ($count-1) % 5 ) {
    echo '<div class="wh-tiles-posts-left2">';
} elseif ( 4 == ($count-1) % 5 ) {
    echo '<div class="wh-tiles-posts-right2">';
}
$this->render_post_body( $count );

if ( 1 != ($count-1) % 5 ) {
    echo '</div>';
} 


来源:https://stackoverflow.com/questions/53542556/repeating-the-rows-within-loop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!