Divide Posts In Sections Of Four (2 posts)

  1. Yojance Rabelo
    Posted 5 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:

    - div with post 1 info
    - div with post 2 info
    - div with post 3 info
    - div with post 4 info
    - div with post 5 info
    - div with post 6 info
    - div with post 7 info

    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
    Posted 5 years ago #

    You don't need to figure out how many li you need in advance. The general code would go something like this

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

Topic Closed

This topic has been closed to new replies.

About this Topic


No tags yet.