Advertisement
alchymyth

index loop 1post/cat

May 8th, 2012
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3. get_header(); ?>
  4.  
  5. <div id="primary">
  6. <div id="content" role="main">
  7.  
  8. <?php
  9. /* Set up an array to hold the ids for the categories we want to pull from
  10. * The order in which the posts are called & displayed is goverened by their
  11. * placement in this array
  12. */
  13. $my_cat_array = array(6613,113,999,1123); //adjust ids to match your current site
  14.  
  15. /*
  16. * Set up a flag that will indicate if we've returned ANY posts from any of the chosen categories
  17. * As we don't have access to the standard Loop 404, we'll need this in case we
  18. * bomb out on all categories. This kind of post counter can come in useful elsewhere...
  19. */
  20. $posts_count = 0;
  21.  
  22. /*
  23. * Rather than modify the main Loop, we'll use get_posts to create 3 secondary loops that grab * the first post from each of these categories. This has the benefit of leaving the main Loop * untouched if you want to call it later on in the page.
  24. */
  25. $cat_posts = array(); //an array to collect the post ids of the one post per category//
  26.  
  27. foreach ( $my_cat_array as $my_cat) :
  28.  
  29. // Set up the secondary loop query
  30. $args = array(
  31. 'numberposts' => 1, // number of posts to return
  32. 'category__in' => array($my_cat), // the category id
  33. 'post__not_in' => $cat_posts // avoid duplicates
  34. );
  35. $lastpost = get_posts($args);
  36.  
  37. // If we have at least 1 post, increment the post_counts flag
  38. if( count( $lastpost ) > 0 ) $posts_count++;
  39.  
  40. // We still have to run a foreach Loop - even if there's only 1 post.
  41. foreach($lastpost as $post) :
  42. $cat_posts[] = $post->ID; //'remember' the post ID
  43. endforeach;
  44. // Move onto the next post in the category array
  45.  
  46. endforeach;
  47. // We've finished the category array!
  48.  
  49. // build a secondary loop with the post IDs from the category posts
  50.  
  51. $args = array(
  52. 'post__in' => $cat_posts,
  53. 'orderby' => 'date',
  54. 'order' => 'DESC',
  55. 'ignore_sticky_posts' => 1
  56. );
  57. $cat_loop = new WP_Query( $args );
  58.  
  59. if( $cat_posts && $cat_loop->have_posts() ) : while( $cat_loop->have_posts() ) : $cat_loop->the_post();
  60. // Now we can display the post using all of the standard Loop tags
  61. ?>
  62. <div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
  63. <h2 class="entry-title"><a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Permalink to %s', 'twentyeleven' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><?php the_title(); ?></a></h2>
  64.  
  65. <div class="entry-content">
  66. <?php the_content( __( 'Continue reading <span class="meta-nav">&rarr;</span>', 'twentyeleven' ) ); ?>
  67. <?php wp_link_pages( array( 'before' => '<div class="page-link">' . __( 'Pages:', 'twentyeleven' ), 'after' => '</div>' ) ); ?>
  68. </div><!-- .entry-content -->
  69.  
  70. <div class="entry-utility">
  71. <?php if ( count( get_the_category() ) ) : ?>
  72. <span class="cat-links">
  73. <?php printf( __( '<span class="%1$s">Posted in</span> %2$s', 'twentyeleven' ), 'entry-utility-prep entry-utility-prep-cat-links', get_the_category_list( ', ' ) ); ?>
  74. </span>
  75. <span class="meta-sep">|</span>
  76. <?php endif; ?>
  77. <?php
  78. $tags_list = get_the_tag_list( '', ', ' );
  79. if ( $tags_list ):
  80. ?>
  81. <span class="tag-links">
  82. <?php printf( __( '<span class="%1$s">Tagged</span> %2$s', 'twentyeleven' ), 'entry-utility-prep entry-utility-prep-tag-links', $tags_list ); ?>
  83. </span>
  84. <span class="meta-sep">|</span>
  85. <?php endif; ?>
  86. <span class="comments-link"><?php comments_popup_link( __( 'Leave a comment', 'twentyeleven' ), __( '1 Comment', 'twentyeleven' ), __( '% Comments', 'twentyeleven' ) ); ?></span>
  87. <?php edit_post_link( __( 'Edit', 'twentyeleven' ), '<span class="meta-sep">|</span> <span class="edit-link">', '</span>' ); ?>
  88. </div><!-- .entry-utility -->
  89. </div><!-- #post-## -->
  90.  
  91. <?php endwhile; wp_reset_postdata();
  92.  
  93. // We now need to check that we did manage to display at least 1 post
  94.  
  95. else :
  96. // Uh oh - we didn't display any posts! ?>
  97. <div id="post-0" class="post error404 not-found">
  98. <h1 class="entry-title"><?php _e( 'Not Found', 'twentyeleven' ); ?></h1>
  99. <div class="entry-content">
  100. <p><?php _e( 'Apologies, but no results were found for the requested archive. Perhaps searching will help find a related post.', 'twentyeleven' ); ?></p>
  101. <?php get_search_form(); ?>
  102. </div><!-- .entry-content -->
  103. </div><!-- #post-0 -->
  104. <?php endif;?>
  105.  
  106. </div><!-- #content -->
  107. </div><!-- #primary -->
  108.  
  109. <?php get_sidebar(); ?>
  110. <?php get_footer();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement