0

I have tried to to make a custom query, to some extend. it is working fine. But where i cannot make it work, is it to break after every 3 posts and do same thing (print div.row) and run same loop, For now, my code seem to output just 3 odd number posts, whereas i want it to break of after each 3 post.

if ($news->have_posts() ) : while ($news->have_posts() ) : $news->the_post();
    //$value = $news->current_post;
echo "<div class='col-lg-4'>";
    if($value % 3 == 0){
        ?>

                        <div class="height <?php foreach(get_the_category() as $category) { echo $category->slug . ' ';} ?>">
                        <div class="text">
                                <h5><?php the_time('F jS, Y'); ?></h5>
                                <h4><a href=""><?php the_title(); ?></a></h4>
                            <?php the_excerpt(); ?>
                        </div>
                    </div>


    <?php 
    //$i++;
    //print($value);
}
echo "</div>";
endwhile; endif;

Edit : What is different about my layout, it should be in indivuial row, as shown in bootly, more like google cards layout.

9
  • @PieterGoosen what you mean – localhost Oct 13 '15 at 9:30
  • @PieterGoosen Thnx, i just updated it. – localhost Oct 13 '15 at 9:38
  • Check the two answers to this question – Pieter Goosen Oct 13 '15 at 9:50
  • @PieterGoosen I tried that now, but my post shows one after another, which isn't what i want. I want layout like [this] (bootply.com/90113) – localhost Oct 13 '15 at 10:12
  • @PieterGoosen my code display right, it is that wrapping of div that is now right, that i want to ask. where i want my div to display like [this] (bootply.com/90113) – localhost Oct 13 '15 at 10:33
0

try this. i am not sure it would work for masonary layout or not. but it would give you an idea on how to make rows and columns within loop.

if ($news->have_posts() ) : while ($news->have_posts() ) : $news->the_post();
    //$value = $news->current_post;

    if($value % 3 == 0){
        ?>
      <div class="row">                 
        echo "<div class='col-lg-4'>";

                        <div class="height <?php foreach(get_the_category() as $category) { echo $category->slug . ' ';} ?>">
                        <div class="text">
                                <h5><?php the_time('F jS, Y'); ?></h5>
                                <h4><a href=""><?php the_title(); ?></a></h4>
                            <?php the_excerpt(); ?>
                        </div>
                    </div>


    <?php 
    //$i++;
    //print($value);
}
echo "</div>";
if($value % 3 !== 0){
echo "<div class='col-lg-4'>";
echo "</div">
}
endwhile; endif;
0

You should just be able to achieve the same effect using CSS and display:flex - I don't think you need to break your loop up into threes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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