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.

9 thoughts on “Feature

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

  2. 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!

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

  4. Pingback: Artemis Fowl FanFiction » Blog Archive » Fix The Comments

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

  6. Pingback: Jazz Up Your Site: 28 Ways To Use WordPress Custom Fields : Performancing

  7. Hi,
    You don great job. We have to make some changes for post.
    //First Loop where we are showing post for cat = 7
    ID; ?>

    // Second loop where we are showing all post
    ID, $feature_post_id)) { continue; } ?>

    Above solution we are got only cat=7 posts top.

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>