WordPress.org

Ready to get started?Download WordPress

Forums

Style first post of the query_posts function (6 posts)

  1. MultiformeIngegno
    Member
    Posted 2 years ago #

    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! ;)

  2. Rev. Voodoo
    Volunteer Moderator
    Posted 2 years ago #

    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

  3. alchymyth
    Forum Moderator
    Posted 2 years ago #

    http://www.transformationpowertools.com/wordpress/styling-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

  4. MultiformeIngegno
    Member
    Posted 2 years ago #

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

    Thanks again!! :)

  5. alchymyth
    Forum Moderator
    Posted 2 years ago #

    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

  6. MultiformeIngegno
    Member
    Posted 2 years ago #

    Thankssss!!! ;)

Topic Closed

This topic has been closed to new replies.

About this Topic