Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

How do I get 3 categories to intermix? WordPress

  • SOLVED

Suppose I have 3 categories:

Lakewood High School

Athens High School

Scottsville High School

Suppose, in the sidebar, that I want show the 7 most recent posts from each category, 21 headlines in all, but it is very important that the items intermix like this:


Lakewood High School

Athens High School

Scottsville High School


Lakewood High School

Athens High School

Scottsville High School


Lakewood High School

Athens High School

Scottsville High School


Lakewood High School

Athens High School

Scottsville High School


Lakewood High School

Athens High School

Scottsville High School


Lakewood High School

Athens High School

Scottsville High School


Lakewood High School

Athens High School

Scottsville High School

What kind of code would I use for this?

Answers (3)

2010-05-21

Andrzej answers:

Try this:

<?php
$my_cats = array( 1, 2, 3 );
$my_maxposts = 7;
foreach ( $my_cats as $cat_id ) {
query_posts('cat=' . $cat_id . '&posts_per_page='. $my_maxposts );
while (have_posts()) : the_post();
ob_start();
?><li><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></li><?php
$my_results[$cat_id][] = ob_get_contents();
ob_end_clean();
endwhile;
wp_reset_query();
}
for ( $i = 0; $i < $my_maxposts; $i ++ ) {
?><ul><?php
foreach ( $my_cats as $cat_id ) {
echo $my_results[ $cat_id ][ $i ];
}
?></ul><?php
}
?>


Tell me if you need any explaination

(soz, small bug, just fixed!)


Andrzej comments:

In case you wanna view also the excerpts after these headlines, you can try this:

<?php
$my_cats = array( 3, 4, 5 ); // define your category IDs here
$my_maxposts = 7; // you can switch the number of last posts here

foreach ( $my_cats as $cat_id ) {
query_posts('cat=' . $cat_id . '&posts_per_page='. $my_maxposts );
while (have_posts()) : the_post();
ob_start();
?>
<li>
<h2><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h2>
<div class="post" id="post-<?php the_ID(); ?>"><?php the_excerpt(); ?></div>
</li>
<?php
$my_results[$cat_id][] = ob_get_contents();
ob_end_clean();
endwhile;
wp_reset_query();
}
for ( $i = 0; $i < $my_maxposts; $i ++ ) {
?><ul><?php
foreach ( $my_cats as $cat_id ) {
echo $my_results[ $cat_id ][ $i ];
}
?></ul><?php
}
?>

2010-05-21

Oleg Butuzov answers:

<?php
/*
Getting the categories
*/
// categories
$catsArray = array('1','2','3');
// prepare variable to get data
$result = array();
// number of poststs to retrive
$counter = 3;
foreach($catsArray as $cat){
query_posts(array('cat' => $cat, 'posts_per_page' => $counter, 'showposts' => $counter));
if (count($wp_query->posts) > 0){
foreach($wp_query->posts as $key=>$_post){
$results[$key][$cat] = $_post;
}
}
wp_reset_query();
}

/*
Retriving the category
*/
foreach($results as $key=>$categories){
echo '<ul>';
foreach($categories as $categoryId=>$post){
$currentCategory = get_category($categoryId);
?>
<li>
<h3><? echo $currentCategory->cat_name;?></h3>
<a href="<?php echo get_permalink($post->ID) ?>"><?php echo apply_filters('the_title', $post->post_title); ?></a>

/*
You also can use
echo apply_filters('the_content', $post->post_content);
to retrive content

------------

echo apply_filters('the_excerpt', $post->post_excerpt);
to retrive content
*/
</li>
<?
}
echo '</ul>';
}
wp_reset_query();
?>

2010-05-21

Monster Coder answers:

<?php
$cats = array('1','2','3'); //enter your 3 category IDs
$num_blocks=7;
$results =array();
foreach ($cats as $cat){
query_posts(array('cat'=>$cat,'posts_per_page'=>$num_blocks));

$i=0;
while (have_posts()): the_post();
if($i<=$num_blocks){
$results[$i][] = get_the_ID();
}
$i++;
endwhile;
wp_reset_query();
}

foreach ($results as $result){

query_posts(array('post__in'=>$result));
while (have_posts()): the_post();
?>
<div class="cats">
<li>

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

<!-- <div class="post" id="post-<?php the_ID(); ?>"><?php the_excerpt(); ?></div> -->

</li>
</div>

<?php
endwhile;
echo '<hr />';
wp_reset_query();
}
?>