Loop Through Wordpress Posts, and Wrap Each X Post in a Div

Loop through WordPress posts, and wrap each X post in a DIV

Most people do this with a modulo operator, but it gets awkward to do it if no posts are found, or and even division occurs on the last post. I've expanded on the answer provided here by @The Shift Exchange to do it in a cleaner way.

<?php
// Get posts (tweak args as needed)
$args = array(
'post_type' => 'page',
'orderby' => 'menu_order',
'posts_per_page' => -1,
'post_parent' => $post->ID,
'order' => 'ASC'
);
$posts = get_posts( $args );
?>

<?php foreach (array_chunk($posts, 2, true) as $posts) : ?>

<div class="row">

<?php foreach( $posts as $post ) : setup_postdata($post); ?>

<a id="post-<?php the_ID(); ?>" <?php post_class(); ?> href="<?php the_permalink(); ?>">
<?php the_post_thumbnail(); ?>
</a>

<?php endforeach; ?>

</div>

<?php endforeach; ?>

You would change the "2" in the first foreach loop to be the amount you want grouped per row.

Wrap every 4 posts in a custom wordpress loop with a div

This should sort you out

$args = array(
'post_type' => 'college',
'posts_per_page' => -1,
'order' => 'DESC',
'orderby' => 'menu_order'
);

$the_query = new WP_Query($args);
if ($the_query->have_posts()) :
$counter = 0;
while ($the_query->have_posts()) : $the_query->the_post();
if ($counter % 4 == 0) :
echo $counter > 0 ? "</div>" : ""; // close div if it's not the first
echo "<div class='row'>";
endif;
?>
<div class="col-3">
<?php the_title(); ?>
</div>
<?php
$counter++;

endwhile;
endif;
wp_reset_postdata();
?>

Adapted from Wrapping a div around every third item in a foreach loop PHP

Wordpress Loop: how to wrap each 3 posts into a div?

Thanks for your support guys! :)
I tried both solutions but didn't work,
I ended up with this and works perfectly!

<?php query_posts('cat=6'); ?>

<?php $variable=0;?>

<div>
<?php while ( have_posts() ) : the_post(); ?>
<?php if(($variable+1)<4){ ?>
<a href="<?php echo get_post_meta($post->ID, 'colaborador-link', true); ?>" target="blank">
<?php the_post_thumbnail(); ?>
</a>
<?php $variable+=1; ?>
<?php }else{ ?>
<?php $variable=1; ?>
</div>

<div>
<a href="<?php echo get_post_meta($post->ID, 'colaborador-link', true); ?>" target="blank">
<?php the_post_thumbnail(); ?>
</a>
<?php }?>
<?php endwhile; ?>
</div>

How can I wrap each 3 posts in a div with category names included?

Ok, this is a working code:

<?php
$taxonomy = 'category';
$param_type = 'category__in';
$term_args=array(
'orderby' => 'name',
'order' => 'DESC',
'child_of' => 4,
);
$terms = get_terms($taxonomy,$term_args);
if ($terms) {
$i = 0;
echo "<div>\n";
foreach( $terms as $term ) {
$args=array(
"$param_type" => array($term->term_id),
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => -1,
'caller_get_posts'=> 1
);
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
$h1 = '<h1>' . $term->name . '</h1>' ;
while ($my_query->have_posts()) : $my_query->the_post();
if ($h1) {
if (++$i % 3 == 1 && $i > 1) {
echo "</div><div>\n";
}
echo $h1;
$h1 = '';
}
if(++$i % 3 == 1) {echo "</div><div>\n";} ?>
<a href="<?php the_permalink(); ?>"> <?php the_post_thumbnail(); ?> </a>
<?php
endwhile;
}
}
echo "</div>\n";
}
wp_reset_query();
?>

Wordpress - Wrap every 2 posts

You actually answered your questions already by yourself with this part of code doesn't work
You want to open the <div> and close it on the same condition...

<?php $counter = -1; ?>
<?php while ( have_posts() ) : the_post();?>
<?php $counter++;?>
<?php if ($counter % 2 == 0) : ?>
<?php echo '<div class="row">'; ?>
<?php endif; ?>
<?php get_template_part( 'template-parts/two-columns', get_post_format() );?>
<?php if ($counter % 2 == 0) : ?>
<?php echo '</div>'; ?>
<?php endif; ?>
<?php endwhile; ?>


Related Topics



Leave a reply



Submit