Creating a feature post or sticky post in a WordPress blog
To get a post from a specific category to appear at the top of the page above the rest of the posts, and to only change when a new post from that particular category is added, use the following code:
<?php /*?><?php $my_query = new WP_Query('category_name=featured&showposts=1');
while ($my_query->have_posts()) : $my_query->the_post();
$do_not_duplicate = $post->ID;?>
<div class="featureItem">
<h1>Feature: <a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></h1>
<p><?php the_time('F j, Y'); ?> • By <?php the_author_firstname(); ?> <?php the_author_lastname(); ?><br />Tags: <?php STP_PostTags(); ?> | <?php comments_popup_link('Leave a Comment', '1 Comment', '% Comments'); ?></p>
<?php the_content('Read more»'); ?>
</p>
<!--
<?php trackback_rdf(); ?>
-->
</div>
<?php endwhile; ?>
<?php if (have_posts()) : while (have_posts()) : the_post(); if( $post->ID == $do_not_duplicate )
continue;?><?php */?>
Obviously, many things above can be changed, like how the titles, categories, tags, etc. are displayed.
Category: Code Snippets










Two comments:
1. Tags, when using WordPress 2.3 or higher, you should use the_tags() instead of STP_PostTags();
2. Dates, when using the_time(‘F j, Y’); you override the user choice of date defined in the general-options part of the wordpress administration panel. I’s use the_date() and the_time() without parameters in order to use the users choice.
Hannit – thanks for your comments. You’re of course right about the tags code. I just cut and paste from a site that still runs on 2.2.
As for your comment about the date, that’s a very interesting point that I never thought about! I’d like to write code snippet about that, and I’ll give you attribution, of course.
you’re very welcome.
writing this snippet sounds good. would love to see it.