logo

$20
How do I get 3 categories to intermix?

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?

Jake Jackson | 05/20/10 at 8:00pm | Edit


(3) Possible Answers Submitted...

  • avatar
    Last edited:
    05/25/10
    10:29pm
    Andrzej Zglobica says:

    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!)

    Previous versions of this answer: 05/20/10 at 8:18pm

    • 05/20/10 9:09pm

      Andrzej Zglobica says:

      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
      }
      ?>

  • avatar
    Last edited:
    05/20/10
    8:40pm
    Oleg Butuzov says:

    <?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();
    ?>

    Previous versions of this answer: 05/20/10 at 8:40pm

  • avatar
    Last edited:
    05/20/10
    11:09pm
    Monster Coder says:

    <?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();
    }
    ?>

    Previous versions of this answer: 05/20/10 at 11:09pm

This question has expired.





Current status of this question: Completed