Feature

As a new set of Posts are published, there’s sometime one Post that deserves extra visibility. This distinct Post will receive permanent exposure as long as no one Post replaces it.

This feature Post exhibits more prominent display and steadily covers up the front page.

This application of Loop resembles that of Miniblog. The featured Post will be tagged (read: categorized) with a reserved category, say, category Feature. This Post will also be tagged with the other regular category–this, bring up an issue. A query_posts() that excludes a specific category will still retrieve any Posts of that specific category if the Posts are also tagged with some other category. In Feature application, the featured Post is expected to not show up on the regular Posts listing on the front page.

  1. <?php get_header(); ?>
  2.  
  3. <div id="content">
  4.  
  5. <?php $feature_post = get_posts( 'category=7&numberposts=1' ); ?>
  6. <?php if( $feature_post ) : ?>
  7.  
  8. <div id="feature" class="post">
  9. <?php foreach( $feature_post as $post ) : setup_postdata( $post ); ?>
  10. <?php $feature_post_id = $post->ID; ?>
  11. <h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a></h2>
  12. <div class="entry">
  13. <?php the_content( '...' ); ?>
  14. </div>
  15. <?php endforeach; ?>
  16. </div>
  17.  
  18. <?php endif; ?>
  19.  
  20.  
  21. <?php if (have_posts()) : ?>
  22.  
  23. <?php while (have_posts()) : the_post(); ?>
  24.  
  25. <?php if( $post->ID == $feature_post_id ) continue; ?>
  26.  
  27. <div class="post" id="post-<?php the_ID(); ?>">
  28. <h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a></h2>
  29. <div class="meta"><?php the_category( ', ' ); ?> <?php the_author(); ?> <?php the_time('F jS, Y'); ?></div>
  30. <div class="entry">
  31. <?php the_excerpt(); ?>
  32. </div>
  33. </div>
  34.  
  35. <?php endwhile; ?>
  36.  
  37. <?php else : ?>
  38.  
  39. <div class="error">
  40. <h2>Not Found</h2>
  41. <p>Sorry, but you are looking for something that isn't here.</p>
  42. <?php include (TEMPLATEPATH . "/searchform.php"); ?>
  43. </div>
  44.  
  45. <?php endif; ?>
  46.  
  47. </div>
  48.  
  49. <?php get_footer(); ?>

Line 5 retrieves the featured Post via get_posts() by passing category of 7, which is the category id of category Feature (Fig. 1), and numberposts of 1. Line 9-15 proceeds with displaying the retrieved Post.

Feature category id

Fig. 1

Note that on line 13, the Post content is displayed with the Template Tag the_content(). This could result in a lenghty text unless the Post content is cut up with the more (<!--more-->) tag. The the_content() Template Tag may be replaced with the_excerpt(), but, it will produce over one hundred characters, which is still quite long.

Line 21-45 deals with the default query_post()–the one initiated with every page load. The query_posts() is not going to be modified and re-initiated to filter category Feature because of the loophole mentioned before. Instead, the resulting set of Posts are processed with some other form of filtering.

Notice that within the previous Loop on line 10, the post id of the featured Post is saved in a variabe $feature_post_id. Within the second Loop on line 25, the post id of current Post processed is checked against the value of $feature_post_id. If it is a match, then that Post will not be further processed and the Loop proceeds with the subsequent Post. With this form of filtering, the feature Post will only show up in one place in the front page.

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.

Miniblog

Miniblog is another variant of Asides. This application of Loop set aside all Posts of a specific category in a space separate from the rest of the Posts. This aggregation of Posts commonly resides in the sidebar.

  1. <?php get_header(); ?>
  2.  
  3. <div id="mainContent">
  4.  
  5. <?php $wp_query->set( 'cat', '-6' ); ?>
  6. <?php query_posts( '' ); ?>
  7.  
  8. <?php if( have_posts() ) : ?>
  9.  
  10. <?php while( have_posts() ) : the_post(); ?>
  11.  
  12. <div class="post" id="post-<?php the_ID(); ?>">
  13. <h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a></h2>
  14. <div class="meta"><?php the_category( ', ' ); ?> <?php the_author(); ?> <?php the_time('F jS, Y'); ?></div>
  15. <div class="entry">
  16. <?php the_excerpt(); ?>
  17. </div>
  18. </div>
  19.  
  20. <?php endwhile; ?>
  21.  
  22. <?php else : ?>
  23.  
  24. <div class="error">
  25. <h2>Not Found</h2>
  26. <p>Sorry, but you are looking for something that isn't here.</p>
  27. <?php include( TEMPLATEPATH . '/searchform.php' ); ?>
  28. </div>
  29.  
  30. <?php endif; ?>
  31.  
  32. </div>
  33.  
  34. <div id="sideContent">
  35.  
  36. <?php query_posts( 'category_name=Inline Function&showposts=10' ); ?>
  37.  
  38. <?php if( have_posts() ) : ?>
  39.  
  40. <h2>Inline Function</h2>
  41. <ul>
  42. <?php while( have_posts() ) : the_post(); ?>
  43.  
  44. <li id="post-<?php the_ID(); ?>"><?php echo wptexturize( $post->post_content ); ?></li>
  45.  
  46. <?php endwhile; ?>
  47. </ul>
  48.  
  49. <?php endif; ?>
  50.  
  51. </div>
  52.  
  53. <?php get_footer(); ?>

Just like Asides, Miniblog implementation requires the reservation of a category. However, unlike Asides, Miniblog requires two Loops to make it happen.

Line 5 of the code above set the variable $wp_query, which contains the information about the Posts to be displayed and is accessed heavily by the Loop, to have a query variable cat of -6 (minus six).

Miniblog category id
Fig. 1

Category id 6 has corresponding category name of Inline Function (Fig. 1). The minus sign indicates the exclusion of all Posts which belong to that category from being retrieved from the database. In turn, the Loop will never have Posts of that category id and only process the specified number of Posts of other category ids.

Line 6 retrieves the Posts from the database with the updated query variable set on line 5. Line 10-18 has the regular Loops which traverse the Posts recently retrieved.

On line 36, a query to retrieve another set of Posts is initiated again. All the query variables are reset and the category_name and showposts are set to Inline Function and 10. This query retrieves Posts of category Inline Function as many as 10. These Posts are excluded in the preceding query (line 5-6). Note that an identical query could constructed by passing the category id instead of its name. (Passing category id instead of category name reduces 1 query.)

  • <?php query_posts( 'cat=6&showposts=10' ); ?>

Line 42-46 proceeds with the second Loop to extract the Posts recently retrieved.

One caveat about miniblogging: When a Post is given multiple category–the Miniblog-assigned category and some other category, the Post will show up in both Miniblog section and in the other Loop section. Therefore, you may want to assign only one category to a Miniblog Post.

Asides

Popularized by Matt, Asides are now craved by many bloggers. The intention of Asides is to display brief (one-liner) Posts in a certain way, distinct from regular Posts.

Asides are commonly implemented by assigning certain Posts (aside Posts) into the a specific category, which will not be assigned to any other (non-aside) Posts. Moreover, an aside Post is usually enclosed in HTML list item tags (<li>) as part of an unordered list (<ul>).

An instruction has been published and the following is the walkthrough to have Asides in a WordPress installation.

  1. <?php get_header(); ?>
  2.  
  3. <div id="content">
  4.  
  5. <?php if( have_posts() ) : ?>
  6.  
  7. <?php
  8. function merge_lists( $str ) {
  9. return preg_replace( '|</ul>\s*<ul class="asides">|', '', $str );
  10. }
  11. ob_start( 'merge_lists' );
  12. ?>
  13.  
  14. <?php while( have_posts() ) : the_post(); ?>
  15.  
  16. <?php if( in_category( 6 ) && !is_single() ) : ?>
  17.  
  18. <ul class="asides">
  19. <li id="post-<?php the_ID(); ?>"><?php echo wptexturize($post->post_content); ?></li>
  20. </ul>
  21.  
  22. <?php else : ?>
  23.  
  24. <div class="post" id="post-<?php the_ID(); ?>">
  25. <h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a></h2>
  26. <div class="meta"><?php the_category( ', ' ); ?> <?php the_author(); ?> <?php the_time('F jS, Y'); ?></div>
  27. <div class="entry">
  28. <?php the_content(); ?>
  29. </div>
  30. </div>
  31.  
  32. <?php endif; ?>
  33.  
  34. <?php endwhile; ?>
  35.  
  36. <?php else : ?>
  37.  
  38. <div class="error">
  39. <h2>Not Found</h2>
  40. <p>Sorry, but you are looking for something that isn't here.</p>
  41. <?php include( TEMPLATEPATH . '/searchform.php' ); ?>
  42. </div>
  43.  
  44. <?php endif; ?>
  45.  
  46. </div>
  47.  
  48. <?php get_footer(); ?>

The implementation of Asides takes minor modification of the Default Loop. The first thing that needs to be done is to create a specific asides category and note the id of that category.

Asides category id
Fig. 1

In this example (Fig. 1), the Asides category is named Inline Function and it has category id of 6. Thus, inside the Loop on line 16, the Posts of category id of 6 are treated differently from the rest of Posts; line 10-20 shows how aside Posts are to be displayed.

On line 8-11 above, a block of code to merge adjacent unordered lists into one is implemented. Without it, the HTML output will have an unordered list for each aside Posts, which is not ideal.

  • <ul class="asides">
  • <li id="post-6">Morbi <code>++var</code> nonummy ligula sit <code>var++</code> amet wisi aliquet lobortis.</li>
  • </ul>
  • <ul class="asides">
  • <li id="post-7">Sed turpis dui, viverra quis, accumsan in, porttitor ut, wisi <code>protected</code>. Vestibulum id sem semper risus semper interdum.</li>
  • </ul>

What’s better is to merge adjacent aside Posts into one unordered list.

  • <ul class="asides">
  • <li id="post-6">Morbi <code>++var</code> nonummy ligula sit <code>var++</code> amet wisi aliquet lobortis.</li>
  • <li id="post-7">Sed turpis dui, viverra quis, accumsan in, porttitor ut, wisi <code>protected</code>. Vestibulum id sem semper risus semper interdum.</li>
  • </ul>

Default Loop

The Loop, by default (read: the Loop in Default Theme) display the Posts chronologically with the most recent Post at the very top followed by less recent Posts.

The following is a sample of common usage of the Loop in theme’s index.php to have a journal-like WordPress installation.

  1. <?php get_header(); ?>
  2.  
  3. <div id="content">
  4.  
  5. <?php if( have_posts() ) : ?>
  6.  
  7. <?php while( have_posts() ) : the_post(); ?>
  8.  
  9. <div class="post" id="post-<?php the_ID(); ?>">
  10. <h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a></h2>
  11. <div class="meta"><?php the_category( ', ' ); ?> <?php the_author(); ?> <?php the_time('F jS, Y'); ?></div>
  12. <div class="entry">
  13. <?php the_content(); ?>
  14. </div>
  15. </div>
  16.  
  17. <?php endwhile; ?>
  18.  
  19. <?php else : ?>
  20.  
  21. <div class="error">
  22. <h2>Not Found</h2>
  23. <p>Sorry, but you are looking for something that isn't here.</p>
  24. <?php include( TEMPLATEPATH . '/searchform.php' ); ?>
  25. </div>
  26.  
  27. <?php endif; ?>
  28.  
  29. </div>
  30.  
  31. <?php get_footer(); ?>

Line 7 indicates the start of the Loop and line 17 ends it. Inside the Loop, Post data are displayed through the call of several Template Tags such as the_title(), the_category(), the_author(), and the_content(). Any other Post-related Template Tags can be placed within the Loop and the corresponding output will be displayed with each Post.