admin管理员组

文章数量:1296919

Will be pleased for any help! The site is done on bootstrap. I have 3 column site. I want simply to add class clearfix if number of posts % 3 == 0; I can add only through browser DevTools

I have my posts code as fallowing:

$pages = get_posts( array(
'numberposts' => -1,
'category'    => '20',
'orderby'     => 'ID',
'order'       => 'ASC',
'post_type'   => 'post',
));

foreach( $pages as $post ) 
    setup_postdata($post);

I'm doing this way but it doesn't work.

foreach( $pages as $post ) { 
if ($post[0] % 3 == 0) {
    setup_postdata($post.'<\div class="clearfix"\>');
}
else {
    setup_postdata($post);
}

Will be pleased for any help! The site is done on bootstrap. I have 3 column site. I want simply to add class clearfix if number of posts % 3 == 0; I can add only through browser DevTools

I have my posts code as fallowing:

$pages = get_posts( array(
'numberposts' => -1,
'category'    => '20',
'orderby'     => 'ID',
'order'       => 'ASC',
'post_type'   => 'post',
));

foreach( $pages as $post ) 
    setup_postdata($post);

I'm doing this way but it doesn't work.

foreach( $pages as $post ) { 
if ($post[0] % 3 == 0) {
    setup_postdata($post.'<\div class="clearfix"\>');
}
else {
    setup_postdata($post);
}
Share Improve this question edited Aug 29, 2017 at 17:00 Anton Lukin 8875 silver badges17 bronze badges asked Aug 29, 2017 at 16:24 BufferOverrunBufferOverrun 111 bronze badge
Add a comment  | 

1 Answer 1

Reset to default 1

Try to update your second code part with

$count = 1;

foreach( $pages as $post ) { 
  setup_postdata($post);

  // show your post here

  if ($count++ % 3 === 0)
    echo '<div class="clearfix"></div>';

本文标签: phpHow to add clearfix class according to to the posts number