I am trying to get all the posts in a specific category separated into 3 different sections. Each category will be in a different div. When I try doing this, WordPress lists every post in every category in each div. How can I get it so that it will only list the posts in each category? I tried to modify loop.php so that my code would look like this

 <?php  if ( is_archive() || is_search() ) :
 <div class="category">
      <p class="textYellow arrow"><a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Permalink to %s', 'paradiso' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><?php the_title(); ?></a></p>

The idea is that each category will sit in a div floated left so that each div will sit next to each other. So I would have three divs, three categories and each div would only list the posts title in that category.

for example,

div 1: 4 posts div 2: 3 posts div 3: 6 posts

Each div only showing the category posts related to that section.

1 Answer 1


Here is something i have used recently... not too complicated...
First.. we call all categories.
Then.. we get posts inside each category using foreach

 $allcats = get_categories('child_of='.get_query_var('cat')); 

    foreach ($allcats as $cat) :
    $args = array(
    'posts_per_page' => 3, // max number of post per category

    'category__in' => array($cat->term_id)

    $customInCatQuery = new WP_Query($args); 

    if ($customInCatQuery->have_posts()) : 
    echo '<div>';
    echo '<h3>'.$cat->name.'</h3>';
    echo '<ul>';    
    while ($customInCatQuery->have_posts()) : $customInCatQuery->the_post(); ?>

    <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>

echo '</ul></div>'; 

<?php else : 
echo 'No post published in:'.$cat->name;                

Hope this helps...
Have a gr8 day, Sagive.

  • This worked perfectly! Exactly what I needed. Thank you very much for sharing.
    – Jamie
    Sep 28, 2012 at 22:56
  • sure thing mate - glad it works for you.
    – Sagive
    Sep 29, 2012 at 4:18
  • This is just what I needed. I made a few mods to fit my project, but other than that, perfect. Feb 3, 2017 at 15:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.