wordpress отображает вторые три сообщения


Мне нужно отобразить:

Первые три сообщения в первом div из wordpress,

Вторые три сообщения во втором div из wordpress и

Третьи три сообщения в третьем разделе с WordPress.

Как я могу это сделать?

div 1
    --1st post
    --2nd tpost
    --3rd post
div 2
    --4th post
    --5th post
    --6th post
div 3
    --7th post
    --8th post
    --9th post
Author: Deepu Sasidharan, 2015-06-04

2 answers

Вы могли бы сделать это несколькими способами. Поместите данные в массив, а затем chunk() это, и зацикливайтесь на этих кусках.

Или вы можете просто перебрать данные:

<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>
 2
Author: cjhill, 2015-06-04 07:18:13

Надеюсь, что код будет работать для вас.

<?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;
                

?>
 1
Author: Tristup, 2015-06-04 07:01:16