• alexlr

    (@alexlr)


    Let’s say for the sake of argument I’m an absolute idiot and I’ve read the codex on conditional tags and I still am having a bit of trouble so I turn to these forums. Hypothetically, let’s say this is what I want to do:

    On the front page I want to aggregate 3 posts from 3 different categories each (so 9 posts total). I want each of these contained in separate divs (but that’s not really important at this point). So, I have the loop at the top <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

    What would be my next step? Everything I’ve hypothetically done up to this point has caused php errors and prevent the page from loading. Let’s say, for this example, the category IDs for each category are 1, 2 and 3. Any ideas? If you can help and you’re in NYC I’ll buy you a beer or six.

    This would be for WordPress 2.9.2.

Viewing 5 replies - 1 through 5 (of 5 total)
  • uwiuw

    (@uwiuw)

    do you need something like this

    $total_post_per_categories = 3; //
    $cat_id_array = array(70,9,126); //use your own post
    
    foreach ($cat_id_array as $cat_id) { 
    
    	if ($exclude_all_this_post !="") {
    		$exclude = "&exclude=" . $exclude_all_this_post; //exclude query syntax
    	}
    
    	$argument = "numberposts=$total_post_per_categories&category=$cat_id" . $exclude ."";
    	$myposts_based_cat = get_posts($argument);  //all post with exlude post
    
    	if (is_array($myposts_based_cat)) {
    		$output .= $argument . "<br /><br />";
    		foreach ($myposts_based_cat as $post) {
    
    			if ($post === end($myposts_based_cat)) { //end of array
    				$exclude_all_this_post .= $post->ID;
    			} else {
    				$exclude_all_this_post .= $post->ID . ",";
    			}
    
    			$output .=   $post->post_title;
    			$output .=   "[$cat_id] ";
    			$output .=  "[postid : " . $post->ID . "]";
    			$output .= "<br />";
    		}
    	}
    	$output .= "<br />";
    }
    
    echo "$output";
    Thread Starter alexlr

    (@alexlr)

    This is what I ended up doing. It seems to work. Not sure if it’s the best way or the right way but it appears to be working. Any feedback would be appreciated.

    <div class="frontpage">
    <h3>What's New</h3>
    	<?php $the_query = new WP_Query('cat=6&showposts=4&orderby=post_date&order=desc');
    	while ($the_query->have_posts()) : $the_query->the_post();
    	$do_not_duplicate = $post->ID; ?>
    	<div class="story">
    		<?php the_excerpt('Read more...'); ?>
    	</div>
    	<?php endwhile; ?>
    
    	<h3>Ask the Doc</h3>
    	<?php $the_query = new WP_Query('cat=5&showposts=4&orderby=post_date&order=desc');
    	while ($the_query->have_posts()) : $the_query->the_post();
    	$do_not_duplicate = $post->ID; ?>
    	<div class="story">
    		<?php the_excerpt('Read more...'); ?>
    	</div>
    	<?php endwhile; ?>
    </div>
    Thread Starter alexlr

    (@alexlr)

    Well, that worked fine for two loops but I forgot I needed a third. I tried your code above uwiuw but it kept spitting out other data besides the actual post content. Guess I still need some help with this.

    uwiuw

    (@uwiuw)

    i have test it and i think you code is more suitable for any loop. When you choose wp_query then you have ability to make a pagination. so it’s better. ??

    Btw, is it possible to use $do_not_duplicate in the second WP_Query() arguments. so you can make it more bullet proof. so when there a post who has more then on category id (for example, 5 and 6 ) will not show on other looping.

    uwiuw

    (@uwiuw)

    it kept spitting out other data besides the actual post content

    what do you mean ? i don’t understand ??
    Btw, you can debug those $post array in my code use print_r and comment every not useful $output (I’m sorry for my grammatical)

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Aggregate posts from different categories on the front page’ is closed to new replies.