Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

There doesn't seem to be a standard technique for differentiating first/top posts. After looking around, I found this method:

$current_query = new WP_Query('post_type=current&post_status=publish'); 

// Pull out top/first post
$first_post = ( $paged == 0 ) ? $posts[0]->ID : '';

while ($current_query->have_posts()) : $current_query->the_post();

if ($first_post == $post->ID) {
    echo '<div class="post top-post-special" id="post-' . get_the_ID() . '">';
} else {
    echo '<div class="post" id="post-' . get_the_ID() . '">';
}

This relies on $paged (which seems to be a Wordpress built-in) to add the "top-post-special" class in first post as expected. However, when using the following query_post instead of a new WP_Query instance, it no longer works:

$args=array(
          'taxonomy' => 'highlights',
            'term' => 'Featured',
          'post_type' => 'highlights',
        );

query_posts($args); 

$first_post = ( $paged == 0 ) ? $posts[0]->ID : '';         

if ( have_posts()) : while (have_posts()) : the_post();                 

if ($first_post == $post->ID) {
    echo '<div class="post top-post-special" id="post-' . get_the_ID() . '">';
} else {
    echo '<div class="post" id="post-' . get_the_ID() . '">';
}

I thought the second would be analogous to the first, not sure what I'm doing wrong here. Is there a better or standardized way to target the first post? Seems like this would come up a lot.

share|improve this question

3 Answers 3

up vote 4 down vote accepted

You shouldn't need to do any special queries for this. Here is one way to accomplish it

/**
 * conditional check ensures special class only shows on top post on first page.
 * if you want top post on page 2, etc. to have special class, just set $first_post to true
 */
if( (int) get_query_var( 'paged' ) > 1 ){
    $first_post = false;
} else {
    $first_post = true;
}

if ( have_posts()) : while (have_posts()) : the_post();                 

if ( $first_post ) {
    echo '<div class="post top-post-special" id="post-' . get_the_ID() . '">';
    $first_post = false;
} else {
    echo '<div class="post" id="post-' . get_the_ID() . '">';
}
share|improve this answer
    
Thank you, this works fine. –  boomturn Apr 9 '12 at 20:20

A simpler solution:

<?php
if (have_posts()) {
    while (have_posts()) {
        the_post();

        // set class for first post on first page
        $class = (!is_paged() && $wp_query->current_post === 0) ? 'top-post-special' : '';
?>

<div id="post-<?php the_ID(); ?>" <?php post_class( $class ); ?>>

</div>

<?php
    }
}
?>
share|improve this answer
    
I tried testing this but couldn't get post_class to behave. It passed the parameter fine, but for some reason wouldn't output the post class itself. Thank you for the response. –  boomturn Apr 9 '12 at 20:23

you could change the one line to:

$first_post = ( !is_paged() ) ? $posts[0]->ID : '';

or use a different approach:

if ($wp_query->current_post == 0 && !is_paged() ) {
       echo '<div class="post top-post-special" id="post-' . get_the_ID() . '">'; 
} else {
       echo '<div class="post" id="post-' . get_the_ID() . '">'; 
} 
share|improve this answer
    
First one didn't work, but the different approach did. I've given the nod to previous poster, but thank you. –  boomturn Apr 9 '12 at 20:20

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.