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.
<?php get_header(); ?>
<div id="content">
<?php $feature_post = get_posts( 'category=7&numberposts=1' ); ?>
<?php if( $feature_post ) : ?>
<div id="feature" class="post">
<?php foreach( $feature_post as $post ) : setup_postdata( $post ); ?>
<?php $feature_post_id = $post->ID; ?>
<h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a></h2>
<div class="entry">
<?php the_content( '...' ); ?>
</div>
<?php endforeach; ?>
</div>
<?php endif; ?>
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<?php if( $post->ID == $feature_post_id ) continue; ?>
<div class="post" id="post-<?php the_ID(); ?>">
<h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a></h2>
<div class="meta"><?php the_category( ', ' ); ?> <?php the_author(); ?> <?php the_time('F jS, Y'); ?></div>
<div class="entry">
<?php the_excerpt(); ?>
</div>
</div>
<?php endwhile; ?>
<?php else : ?>
<div class="error">
<h2>Not Found</h2>
<p>Sorry, but you are looking for something that isn't here.</p>
<?php include (TEMPLATEPATH . "/searchform.php"); ?>
</div>
<?php endif; ?>
</div>
<?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.
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.