0

I have an HTML markup like bellow-

<div class="f_product_slider slick">
  <div class="row slider_item">
    <div class="col-lg-5 col-sm-6">
        <div class="item">
            <img src="image/bike/cycle_2.png" alt="">
            <div class="content text-right">
                <a href="product-details.html"><h6>Specialized Sirrus Carbon - 2018</h6></a>
                <p>Slayer Bike Expert</p>
            </div>
        </div>
    </div>
    <div class="col-lg-5 col-sm-6">
        <div class="item item_two">
            <img src="image/bike/cycle_2.png" alt="">
            <div class="content text-right">
                <a href="product-details.html"><h6>Specialized Sirrus Carbon - 2018</h6></a>
                <p>Slayer Bike Expert</p>
            </div>
        </div>
    </div>
</div>
<div class="row slider_item">
    <div class="col-lg-5 col-sm-6">
        <div class="item">
            <img src="image/bike/cycle_2.png" alt="">
            <div class="content text-right">
                <a href="product-details.html"><h6>Specialized Sirrus Carbon - 2018</h6></a>
                <p>Slayer Bike Expert</p>
            </div>
        </div>
    </div>
    <div class="col-lg-5 col-sm-6">
        <div class="item item_two">
            <img src="image/bike/cycle_2.png" alt="">
            <div class="content text-right">
                <a href="product-details.html"><h6>Specialized Sirrus Carbon - 2018</h6></a>
                <p>Slayer Bike Expert</p>
            </div>
        </div>
    </div>
  </div>
</div>

Here, every row items containing two items. That's mean, every two loop items rendered in every step of the loop. I'm giving here a visual example of this loop- enter image description here

How can I make it possible with PHP while loop? Please, don't hesitate to ask me for more details if you get confused by the question.

2
  • give us your php code so we can understand more your need hence give you suitable help. Commented Aug 18, 2018 at 12:35
  • It's a WordPress custom post loop. Here is my custom post query- $query = new WP_Query(array( 'post_type' => 'product', 'post_status' => 'publish', 'posts_per_page' => -1 )); Commented Aug 18, 2018 at 12:40

1 Answer 1

1

With for:

<div class="f_product_slider slick">
    <?php for($slideCounter = 0; $slideCounter < 2; $slideCounter++) { ?>
    <div class="row slider_item">
        <?php for($colCounter = 0; $colCounter < 2; $colCounter++) { ?>
        <div class="col-lg-5 col-sm-6">
            <div class="item">
                <img src="image/bike/cycle_2.png" alt="">
                <div class="content text-right">
                    <a href="product-details.html"><h6>Specialized Sirrus Carbon - 2018</h6></a>
                    <p>Slayer Bike Expert</p>
                </div>
            </div>
        </div>
        <?php } ?>
    </div>
    <?php } ?>
</div>

With while:

<div class="f_product_slider slick">
    <?php
        $slideCounter = 0;
        while($slideCounter < 2) {
            $slideCounter++ ?>
    <div class="row slider_item">
        <?php
        $colCounter = 0;
        while($colCounter < 2) {
            $colCounter++ ?>

        <div class="col-lg-5 col-sm-6">
            <div class="item">
                <img src="image/bike/cycle_2.png" alt="">
                <div class="content text-right">
                    <a href="product-details.html"><h6>Specialized Sirrus Carbon - 2018</h6></a>
                    <p>Slayer Bike Expert</p>
                </div>
            </div>
        </div>
        <?php } ?>
    </div>
    <?php } ?>
</div>
Sign up to request clarification or add additional context in comments.

3 Comments

Can you show me the loop in PHP while loop format, please?
Updated answer.
If the while loop condition is 4 ($slideCounter < 4), the inside loop will be divided by 2. That's mean the inside items will be 2 if the total items are 4.

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.