0

i need to create a wordpress loop where the first post will be col-md-12 and the next 4 posts will be col-md-6

<div class= "col-md-12">
</div>
<div class= "col-md-6">
</div>
<div class= "col-md-6">
</div>
<div class= "col-md-6">
</div>
<div class= "col-md-6">
</div>

and then

<div class= "col-md-12">
</div>
<div class= "col-md-6">
</div>
<div class= "col-md-6">
</div>
<div class= "col-md-6">
</div>
<div class= "col-md-6">
</div>
4
  • What have you tried so far? And do you want the list twice? Commented Feb 7, 2019 at 8:13
  • no i want it to repeat itself every time starts with md-12 and the next 4 posts md-6 Commented Feb 7, 2019 at 8:17
  • You can use %5 (modulo operator) Commented Feb 7, 2019 at 8:18
  • the html you add , you have to create one column with 12 and 2 lines with 6 (4 columns of 6 ) is that right Commented Feb 7, 2019 at 8:18

2 Answers 2

0

may be this can help :

<?php 
if ( have_posts() ) {
     $counter = 0;
    while ( have_posts() ) {

    if(($counter%5) == 0 ){?>
        <div class= "col-md-12"> call the loop tags of the curent post to show data that you want </div>
<?php
}
    else{?>

     <div class= "col-md-6"> call the loop tags of the curent post to show data that you want </div>

    <?php }

    $counter++;
    } // end while
} // end if
?>

In case to limit post to 10 post only:

   <?php 
    $wp =  new WP_Query(array('posts_per_page'=>10));
    if ( $wp->have_posts() ) {
         $counter = 0;
        while ( $wp->have_posts() ) {

        if(($counter%5) == 0 ){?>
            <div class= "col-md-12"> call the loop tags of the curent post to show data that you want </div>
    <?php
    }
        else{?>

         <div class= "col-md-6"> call the loop tags of the curent post to show data that you want </div>

        <?php }

        $counter++;
        } // end while
    } // end if
    ?>

you have to call all the tags like : the_title(), the_content() preceded by $wp , but not obligation

Sign up to request clarification or add additional context in comments.

17 Comments

it keeps getting the posts with col-md-12
sorry i have to initial counter before while
perfect it works but the loop never stops it keeps getting posts from the database i need to get only 10 posts
break the loop if the counter reach 10
But then you're still grabbing unneccessary data from the database, better to limit the number of posts in the query with the posts_per_page argument
|
0

Try a ternary operator:

<?php 
if ( have_posts() ) {
    $counter = 0;
    while ( have_posts() ) {    
        $colClass = ($counter%5) == 0 ) ? "col-md-12" : "col-md-6";
        echo '<div class= "' . $colClass . '">';
        // Post data
        echo '</div>';   
        $counter++;
    } // end while
} // end if
?>

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.