WordPress.org

Ready to get started?Download WordPress

Forums

Index with categories and posts in different loops (2 posts)

  1. atchor
    Member
    Posted 1 year ago #

    Hi!

    What I'm trying to achieve is to replace current blog-like index page with this:

    Inside every column of 3-column table there are 3 DIVs, each containing seperate WP_Query for showing 3 excerpts of posts with image.

    <table>
    <tr>
    <td>
    <?php
    $first_query = new WP_Query('cat=1','postcount=3');
    ?>
      <table class="boxedCatTbl">
       <tr>
        <td class="boxedCatTblTtl">
         <h1>
          <?php echo $catName; ?>
         </h1>
        </td>
        <td class="boxedCatTblList">
         <?php while($first_query->have_posts()) : $first_query->the_post(); ?>
         <li>
         <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
         <p>
          <?php the_excerpt(); ?>
         </p>
         </li>
         <?php endwhile; wp_reset_postdata(); ?>
        </td>
        <td class="boxedCatTblAll">
         <a href="">Poka┼╝ wszystkie z kategorii...</a>
        </td>
       </tr>
      </table>
    </div>
    </td>
    <td>
    </td>
    <td>
    </td>
    </tr>
    </table>

    The problem is:
    how to loop 3 DIVs, so that column name and loop number are variables from array.

  2. Spencer Cameron
    Member
    Posted 1 year ago #

    Hm. I'm not sure I follow. What criteria do you have for pulling the posts? Without that, I'm stabbing a bit in the dark, but let's assume you want to pull by category. Maybe this will at least get you on the right track. :D

    Replace your current WP_Query code with this:

    <?php $category_ids = array( 1, 2, 3 ); ?>
    <?php foreach( $category_ids as $category_id ) : ?>
        <?php $query = new WP_Query( array( 'cat' => $category_id, 'posts_per_page' => 3 ) );
        <table>
        ...
        </table>
    <?php endforeach; ?>

    Just do what ever you'd like within the foreach in terms of formatting. This code will run once for each element in $category_ids, in this case 3 times. Does this help at all?

Topic Closed

This topic has been closed to new replies.

About this Topic