How to repeat ul and div after every 9th li

I want to repeat <div>and <ul> after every 9th record. But not getting exact result. Please help to sort out the issue.

<div class="col1">
<div class="mylist">
    <ul>
        <li class="test1"><a href="#"></a></li>
        <li class="test1"><a href="#"></a></li>
        <li class="test1"><a href="#"></a></li>
        <li class="test1"><a href="#"></a></li>
        <li class="test1"><a href="#"></a></li>
        <li class="test1"><a href="#"></a></li>
        <li class="test1"><a href="#"></a></li>
        <li class="test1"><a href="#"></a></li>
        <li class="test1"><a href="#"></a></li>
    </ul>
</div>
</div>  

<?php 
                    $args1 = array(
                    'post_type' => 'newser',
                    'orderby'   => 'id', 
                    'order'     => 'ASC',
                    'tax_query' => array(
                        array(
                        'taxonomy' => 'service',
                        'field' => 'id',
                        'terms' => $term_id
                         )
                      )
                    );
                    $query1 = new WP_Query( $args1 ); 
                    $counter = 1;
                    while ($query1->have_posts()) : $query1->the_post(); 

                    ?>

                    <div class="col1">
                        <div class="mylist">
                            <ul>


                             <li class="test<?php echo $counter; ?>"><a href="#">
                             <?php the_title(); ?>
                            </a>
                                </li>
                            <?php  
                            if($counter % 9 == 0)
                            {
                                echo '</ul></div></div><div class="col-sm-4"><div class="list-part"><ul>';
                            }
                            ?>

                          <!--  </ul>
                        </div>
                    </div>-->

                <?php $counter++; endwhile; ?>  

Related posts

4 comments

  1. <div class="col1">
        <div class="mylist">
            <ul>
                <?php
                for ($i = 0; $i < 20; $i++) {
                    ?>
                    <li class="test1"><a href="#"><?= $i ?></a></li>
                    <?php if ($i % 9 == 0 && $i > 0) { ?>
                    </ul>
                </div>
            </div> 
            <div class="col1">
                <div class="mylist">
                    <ul>
                    <?php } ?>
                <?php } ?>    
                <?php if ($i % 9 != 0 || !$i) { ?>
                </ul>
            </div>
        </div> 
    <?php } ?>
    
  2. Try this type of code..

    <div class="col1">
        <div class="mylist">
            <ul>
            <?php 
            for($i=1;$i<=20;$i++){
                if($i==9){
                    echo '</ul></div></div>';
                    echo '<div class="col1"><div class="mylist"><ul>';
                }
                echo '<li class="test1"><a href="#"></a></li>';
            }
            ?>
            </ul>
        </div>
    </div>
    

    Hope this helps.

  3. Hey if you are doing php so below code will work for you:

    <?php
    for ($i = 0; $i < 180; $i++) {
        ?>
        <div class="col1">
            <div class="mylist">
                <ul>
                    <li class="test1"><a href="#"></a></li>
                    <li class="test1"><a href="#"></a></li>
                    <li class="test1"><a href="#"></a></li>
                    <li class="test1"><a href="#"></a></li>
                    <li class="test1"><a href="#"></a></li>
                    <li class="test1"><a href="#"></a></li>
                    <li class="test1"><a href="#"></a></li>
                    <li class="test1"><a href="#"></a></li>
                    <li class="test1"><a href="#"></a></li>
                    <?php if ($i % 9 == 0) { ?>
                    </ul>
                </div>
            </div> 
            <div class="col1">
                <div class="mylist">
                    <ul>
                    <?php } ?>
                </ul>
            </div>    
        <?php } ?>    
    
        ?>
    

    I think that will be work.

  4. You can write a for-loop that has conditions for the various special conditions, e.g. handling the first element (need the starting <ul>), every ninth position (your question), and the last element (need the closing </ul>).

    for($i=1; $i<=$counter; $i++){
        if($i==1){
            echo '<ul><li>gdgdfgdfg</li>';
        } else if($i%9==0 && $i>1 && $i<$counter){
            echo '<li>gdgdfgdfg</li></ul><ul>';
        } else if($i==$counter){
            echo '<li>gdgdfgdfg</li></ul>';
        } else{
            echo '<li>gdgdfgdfg</li>';
        }
    }
    

Comments are closed.