WordPress.org

Support

Support » How-To and Troubleshooting » Style first post of the query_posts function

Style first post of the query_posts function

  • Hi! 🙂
    I’m looking for a way to add a class to the first entry only of the query_posts function..

    Here’s the code I’m using:

    <?php
    // The Query
    query_posts( 'cat=170&showposts=3' );
    
    // The Loop
    while ( have_posts() ) : the_post();
    	echo '
    	<h3 style="font-size:14px"><a href="';the_permalink(); echo '" title="';the_title(); echo '"> '; the_title(); echo '</a></h3> <span style="color:#6e6e6e">'; the_excerpt(); echo '</span>';
    endwhile;
    
    // Reset Query
    wp_reset_query();
    
    ?>

    I think it’s something like if($x == 1) { first post } else { other posts } (as per this post: http://wordpress.org/support/topic/query_posts-showposts-dont-show-first-post), but I don’t know how to “use it”.. where should I put it?

    Thanks in advance! 😉

Viewing 5 replies - 1 through 5 (of 5 total)
  • http://wordpress.org/support/topic/styling-the-first-post?replies=6

    It’s not done with the query, just add a counter for your loop

    Moderator Michael

    @alchymyth

    Styling the First Post Different

    add a class to the first entry

    to which html tag?

    example with the class on the h3 tag:

    while ( have_posts() ) : the_post();
    	echo '
    	<h3 <?php if( $wp_query->current_post ==0 && !is_paged() ) echo ' class="first-post" '; ?> style="font-size:14px"><a href="';the_permalink(); echo '" title="';the_title(); echo '"> '; the_title(); echo '</a></h3> <span style="color:#6e6e6e">'; the_excerpt(); echo '</span>';
    endwhile;

    edit:
    missing $ before wp_query and () added after is_paged() -> the code above is corrected

    Thanks alchymyth!! ^_^
    And how to apply the class instead to all the other entries except the first (the contrary)?

    Thanks again!! 🙂

    Moderator Michael

    @alchymyth

    edit: I had two mistakes in the first reply – you might want to try the corrected code again, or try the new one.

    try a negated if statement (also corrrected the missing brackets after is_paged);

    <h3 <?php if( $wp_query->current_post !=0 || is_paged() ) echo ' class="not-first-post" '; ?> style="font-size:14px"><a href="';the_permalink(); echo '" title="';the_title(); echo '"> '; the_title(); echo '</a></h3> <span style="color:#6e6e6e">'; the_excerpt(); echo '</span>';

    http://codex.wordpress.org/Function_Reference/is_paged

    Thankssss!!! 😉

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Style first post of the query_posts function’ is closed to new replies.