List 3 items side by side

Asked

Viewed 297 times

1

I want to make a list of sequential items only that with 3 columns, I created an odd or even logic, to do the listing side by side, in case 2 columns...

But now I have to make a list of 3 items per line... The image below shows how the idea is, but only that column 3 is repeating the items in column 2.

inserir a descrição da imagem aqui

<div class="row p30-top">
                    <div class="col-md-12">
                        <?php
                            if(have_posts()) : the_post();
                                $i = 0;
                                $content = explode("<li>", get_the_content());
                                foreach($content as $row) : 
                                    if($i % 2 == 0) : 
                        ?>
                                <div class="row">
                                    <div class="col-md-4"><?= $row ?></div>
                        <?php else : ?>
                                    <div class="col-md-4"><?= $row ?></div>
                                    <div class="col-md-4"><?= $row ?></div>
                                </div>
                        <?php
                                    endif;
                                    $i++;
                                endforeach;
                                if($i % 2 != 0) { echo '</div>'; }
                            endif;
                        ?>
                    </div>
                </div>

Exemplo de como estou implementando o código:. (Acima)

1 answer

1

You can use the function arr_chunk to break the array into pieces of 3:

arr_chunk(array,quantidade);

So you already create the structure and do not deal with the contents:

<div class="row p30-top">
<div class="col-md-12">
    <?php
        if(have_posts()) : the_post();
        $content = explode("<li>", get_the_content());
        $content = array_chunk($content,3);

        foreach($content as $row){
            echo '<div class="row">';
            foreach($row as $col){
                echo  '<div class="col-md-4">' . $col . '</div>';
            }
            echo '</div>';
        }
    ?>
</div>

That should solve.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.