Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

 //loop.php
 <?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>
 </div>
 endif;

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.

share|improve this question
add comment

1 Answer

up vote 0 down vote accepted

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

<?php  
 $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>

<?php
endwhile; 
echo '</ul></div>'; 
?>

<?php else : 
echo 'No post published in:'.$cat->name;                
endif; 
wp_reset_query();
endforeach; 
?>

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

share|improve this answer
    
This worked perfectly! Exactly what I needed. Thank you very much for sharing. –  Jamie Sep 28 '12 at 22:56
    
sure thing mate - glad it works for you. –  Sagive SEO Sep 29 '12 at 4:18
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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