WordPress.org

Support

Support » Plugins and Hacks » Hacks » Divide Posts In Sections Of Four

Divide Posts In Sections Of Four

  • Hi everyone, I’m having problems beyond my PHP skills trying to accomplish something with WordPress and I hope one of the forum members can help me out by offering me some sample code or an article.

    I’m trying to display a few custom fields for every post under a category. I’m using jCarouselLite to display a slider with 4 posts at a time.

    I’m having trouble working the logic to make this work. I need to put 4 posts inside every list item. So I need to do this:

    <ul>
    <li>
    - div with post 1 info
    - div with post 2 info
    - div with post 3 info
    - div with post 4 info
    </li>
    <li>
    - div with post 5 info
    - div with post 6 info
    - div with post 7 info
    </li>
    </ul>

    Another problem that arises is that I need to know how many li I will need to have depending on how many posts that category has.

    If anyone could help me, I would be forever in debt to you!
    Thanks for reading….

Viewing 1 replies (of 1 total)
  • You don’t need to figure out how many li you need in advance. The general code would go something like this

    $postPerGroup=4;
    $currentCount=0;
    
    $output='';
    foreach ($posts as $post) {
      if ( ( $currentCount % $postPerGroup ) == 0 ) $output.='<li>';
      $output.=your_function_to_get_the_post_info();
      $currentCount++;
      if ( ( $currentCount % $postPerGroup ) == 0 ) $output.='</li>';
    }
    
    echo '<ul>', $output, '</ul>';

Viewing 1 replies (of 1 total)
  • The topic ‘Divide Posts In Sections Of Four’ is closed to new replies.