Grouped by Category

Simply displaying a list of categories may not attract readers to go further clicking the links. To give equal exposure to older, noteworthy Posts in other categories, especially when there is sparse posting in that category, another view of Posts is needed.

Grouping by category lays out Posts of same category under one heading, the category name.

  1. <?php get_header(); ?>
  2.  
  3. <div id="content">
  4.  
  5. <?php $posts = get_posts( "category=2&numberposts=3" ); ?>
  6. <?php if( $posts ) : ?>
  7.  
  8. <div class="section" id="modernScience">
  9. <h2>Modern Science</h2>
  10. <ul>
  11. <?php foreach( $posts as $post ) : setup_postdata( $post ); ?>
  12.  
  13. <li><span class="date"><?php the_time('F j'); ?></span> <a href="<?php the_permalink() ?>" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a></li>
  14.  
  15. <?php endforeach; ?>
  16. </ul>
  17. </div>
  18.  
  19. <?php endif; ?>
  20.  
  21. <?php $posts = get_posts( "category=3&numberposts=3" ); ?>
  22. <?php if( $posts ) : ?>
  23.  
  24. <div class="section" id="lifestyle">
  25. <h2>Lifestyle</h2>
  26. <ul>
  27. <?php foreach( $posts as $post ) : setup_postdata( $post ); ?>
  28.  
  29. <li><span class="date"><?php the_time('F j'); ?></span> <a href="<?php the_permalink() ?>" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a></li>
  30.  
  31. <?php endforeach; ?>
  32. </ul>
  33. </div>
  34.  
  35. <?php endif; ?>
  36.  
  37. <?php $posts = get_posts( "category=4&numberposts=3" ); ?>
  38. <?php if( $posts ) : ?>
  39.  
  40. <div class="section" id="softwareDesign">
  41. <h2>Software Design</h2>
  42. <ul>
  43. <?php foreach( $posts as $post ) : setup_postdata( $post ); ?>
  44.  
  45. <li><span class="date"><?php the_time('F j'); ?></span> <a href="<?php the_permalink() ?>" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a></li>
  46.  
  47. <?php endforeach; ?>
  48. </ul>
  49. </div>
  50.  
  51. <?php endif; ?>
  52.  
  53. <?php $posts = get_posts( "category=5&numberposts=3" ); ?>
  54. <?php if( $posts ) : ?>
  55.  
  56. <div class="section" id="spaghettiCode">
  57. <h2>Spaghetti Code</h2>
  58. <ul>
  59. <?php foreach( $posts as $post ) : setup_postdata( $post ); ?>
  60.  
  61. <li><span class="date"><?php the_time('F j'); ?></span> <a href="<?php the_permalink() ?>" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a></li>
  62.  
  63. <?php endforeach; ?>
  64. </ul>
  65. </div>
  66.  
  67. <?php endif; ?>
  68.  
  69. <?php $posts = get_posts( "category=6&numberposts=10" ); ?>
  70. <?php if( $posts ) : ?>
  71.  
  72. <div class="section" id="inlineFunction">
  73. <h2>Inline Function</h2>
  74. <ul>
  75. <?php foreach( $posts as $post ) : setup_postdata( $post ); ?>
  76.  
  77. <li><?php echo wptexturize($post->post_content); ?></li>
  78.  
  79. <?php endforeach; ?>
  80. </ul>
  81. </div>
  82.  
  83. <?php endif; ?>
  84.  
  85. </div>
  86.  
  87. <?php get_footer(); ?>

The code above shows Loops as many as five. Each Loop is responsible for processing a set of Posts of a different category (Fig. 1); each set of Posts is first retrieved by get_posts() .

Categories list

Fig. 1

Line 5 fetches the first set of Posts. It passes to get_posts() the category of 2–the category id of Modern Science–and numberposts of 3. The variable $posts, then, contains at most 3 latest Posts of category Modern Science.

Line 11-15 loops thru the previously set variable $posts (Line 5) and output the desired corresponding Post data on line 13.

The rest of the code is simply the repeat of the first Posts retrieval and Loop (line 5-19) with different categories.

The last Loop (line 69-83), however, is treated slightly differently. Instead of displaying the link to the Post, which all previous Loops do, the last Loop displays the content of the Post (line 77). The last Loop process the category Inline Function which acts as regular news updates, whereas Posts of other categories are to be published sparingly.

51 thoughts on “Grouped by Category

  1. hello!,I love your writing very much! percentage we communicate more approximately your post on AOL?
    I require a specialist in this area to solve my
    problem. May be that’s you! Taking a look forward to see you.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>