Wordpress Loop Group каждые два поста

Я ищу какое-то решение, чтобы обернуть два сообщения в группе.

Пример группы будет таким:

<div class="group1">
 - Post Title 1
 - Post Title 2
</div>
<div class="group2">
- Post Title 3
- Post Title 4
</div>
<div class="group1">
 - Post Title 5
 - Post Title 6
</div>
<div class="group2">
- Post Title 7
- Post Title 8
</div>

Пожалуйста, посоветуйте мне хорошее решение.

С уважением

1 ответ

Используйте это, позвоните the_post() внутри цикла, чтобы выполнить два поста в каждом цикле.

$inc_tmp=1;
// The Query
$the_query = new WP_Query( $args );

// The Loop
if ( $the_query->have_posts() ) {
    while ( $the_query->have_posts() ) {
        $the_query->the_post();
        ?>
        <div class="group<?php echo $inc_tmp;?>">
            <?php the_title(); ?>
            <?php $the_query->the_post(); /*  Advance onto the next post, and set the global $post variable. */?>
            <?php the_title(); ?>
        </div>
        <?php
        $inc_tmp++;
    }
} else {
    // no posts found
}
/* Restore original Post Data */
wp_reset_postdata();

Обновление: если вы используете запрос по умолчанию, следуйте этому коду

$inc_tmp=1;
// The Loop
if ( have_posts() ) {
    while ( have_posts() ) {
        the_post();
        ?>
        <div class="group<?php echo $inc_tmp;?>">
            <?php the_title(); ?>
            <?php the_post(); /*  Advance onto the next post, and set the global $post variable. */?>
            <?php the_title(); ?>
        </div>
        <?php
        $inc_tmp++;
    }
} else {
    // no posts found
}
Другие вопросы по тегам