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.
Is there a way to make this work off of the post_id, instead of it being dependant off of categories?
I am thinking that it could display the most current post, and then in the second loop, display the list of posts again, but tell it to not repeat the post from the previous loop.
-jeremy
Great site by the way!
I have put the Feature Post code in my index.php. The first part is well diplaying the Featured Posts but the 2nd part just hide the posts of the category in the normal loop. Will you please suggest What & where? a code to be inserted in my normal loop as mentioned below:
“>/images/.jpg” border=”0″ alt=”More Similar Articles..”>
“>
” rel=”bookmark” title=”Permanent Link to “>
..read more »‘); ?>
I am sorry for these above bulky entries!
Earlier I had tried for submitting my problem with codes. But, here I see the Codes are stripped. Anyway can I get help on this feature post trick from you? Can I send detail by email to you? Please reply a.s.a.p. Thanks & regards
Pingback: Artemis Fowl FanFiction » Blog Archive » Fix The Comments
How to do this? I want make my featured post showing up 3 latest featured and on the next loop will not display my showing 3 latest post but display post after featured loop.
Please Help me
Has anyone tried to incorporate a thumbnail into this script?
Pingback: Jazz Up Your Site: 28 Ways To Use WordPress Custom Fields : Performancing