Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

wordpress display second three posts

I need to display:

first three posts in first div from wordpress,

second three posts in second div from wordpress and

third three posts in third div from wordpress.

How can I do this?

div 1
    --1st post
    --2nd tpost
    --3rd post
div 2
    --4th post
    --5th post
    --6th post
div 3
    --7th post
    --8th post
    --9th post
like image 985
User Avatar asked Mar 01 '26 01:03

User


2 Answers

You could do it via several methods. Put the data into an array and then chunk() it, and loop over those chunks.

Or, you could simply loop over the data:

<div class="...">
    <?php
    // Set counter
    $i = 1;

    // Loop over your posts
    while (have_posts()) {
        // If you have ouputted three already then close and reopen div
        if ($i == 3) {
            $i = 1;

            echo '</div><div class="...">';
        }

        // Output the post and increment the counter
        the_content();
        $i++;
    }
    ?>
</div>
like image 111
cjhill Avatar answered Mar 02 '26 14:03

cjhill


Hope the code will work for you.

<?php
$i=0;
if (have_posts() ) : while ( have_posts() ) : the_post();  

if($i==0)
{
?>
<div><!--open div-->
<?php
}
?>
<h1><?php the_title(); ?></h1>
<p><?php the_content(); ?></p>
<
$i++;
if($i==3)
{
$i=0;
?>
</div><!--closing div -->  
<?php
}
endwhile; endif;
                

?>
like image 26
Tristup Avatar answered Mar 02 '26 15:03

Tristup



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!