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.

 

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

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.