Advertisement
alchymyth

1 post per cat with excludes

Nov 1st, 2011
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.08 KB | None | 0 0
  1. <?php // cycle through categories, print 1 post for each category
  2.  
  3. $exclude = '1'; //exclude 'uncategorized'//
  4. $lastpost = get_posts('numberposts=1'); //get the last post
  5. $last_cats = get_the_category($lastpost[0]->ID); //get the categor(y/ies) from the last post and add to the exclude list//
  6. foreach($last_cats as $last_cat) {
  7. $exclude .= ','.$last_cat->term_id;
  8. }
  9. $categories=get_categories('orderby=name&order=ASC&exclude='.$exclude);
  10.  
  11.   foreach($categories as $category) {
  12.       $posts=get_posts('showposts=1&cat='. $category->term_id);
  13.       if ($posts) {
  14.         echo '<p>Category: <a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a> </p> ';
  15.         foreach($posts as $post) {
  16.           setup_postdata($post); ?>
  17.           <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
  18.           <?php
  19.         } // foreach($posts
  20.       } // if ($posts
  21.     } // foreach($categories
  22. ?>
  23.  
  24.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement