WordPress.org

Forums

Divide Posts In Sections Of Four (2 posts)

  1. Yojance Rabelo
    Member
    Posted 4 years ago #

    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....

  2. Xephan
    Member
    Posted 4 years ago #

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

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.