• Resolved EnergyFreak

    (@energyfreak)


    Hi,

    I would like to add “last” to <article class=”post”> to be this <article class”post last”>

    Here is what I have so far:

    <?php $most_popular_query = new WP_Query( array( 'category_name' => 'player-news', 'posts_per_page' => 1 ) );
    while ( $most_popular_query->have_posts() ) : $most_popular_query->the_post(); ?>
    
    <article class="post">
    
    <div class="thumb"><a href="<?php the_permalink() ?>"><?php the_post_thumbnail( 'frontpage-most-popular-thumb' ); ?></a></div>
    
    <div class="cat"><?php the_category(', ') ?></div>
    
    <h3><a href="<?php the_permalink() ?>" title="<?php the_title_attribute(); ?>"><?php echo(get_the_title()); ?></a></h3>
    
    <div class="activity">
    <span class="views">12</span><span class="comment"><a href="#"><?php $comments_count = wp_count_comments(post_id);echo $comments_count->total_comments;?></a></span>
    </div>
    </article><!--  /.post -->
    
    <?php endwhile; 
    
    // Reset Query
    wp_reset_query();
    ?>
    
    <article class="post last">
    <div class="thumb">
    <a href="#"><img src="<?php echo esc_url( get_template_directory_uri() ); ?>/images/thumbs/7.jpg" alt="img"></a>
    </div>
    <div class="cat">
    <a href="">Social media</a>
    </div>
    <h3><a href="#">I'm not claiming the multiplier is precisely 36, but it is certainly more than 10...</a></h3>
    <div class="activity">
    <span class="views">12</span><span class="comment"><a href="#">0</a></span>
    </div>
    </article><!--  /.post -->

    I want to know how I can add a function inside my while loop to do this on the last post. Also, to make it easier, I don’t need the function to count current posts and determine which one is the last one. Essentially, there will be two posts in this loop, so I need to apply “last” to the class of the second post.

    Thank you!

Viewing 3 replies - 1 through 3 (of 3 total)
  • replace your posted code section with:

    <?php $most_popular_query = new WP_Query( array( 'category_name' => 'player-news', 'posts_per_page' => 2 ) );
    while ( $most_popular_query->have_posts() ) : $most_popular_query->the_post(); ?>
    
    <article class="post<?php if( $most_popular_query->current_post == $most_popular_query->post_count-1 ) echo ' last'; ?>">
    
    <div class="thumb"><a href="<?php the_permalink() ?>"><?php the_post_thumbnail( 'frontpage-most-popular-thumb' ); ?></a></div>
    
    <div class="cat"><?php the_category(', ') ?></div>
    
    <h3><a href="<?php the_permalink() ?>" title="<?php the_title_attribute(); ?>"><?php echo(get_the_title()); ?></a></h3>
    
    <div class="activity">
    <span class="views">12</span><span class="comment"><a href="#"><?php $comments_count = wp_count_comments(post_id);echo $comments_count->total_comments;?></a></span>
    </div>
    </article><!--  /.post -->
    
    <?php endwhile; 
    
    // Reset Query
    wp_reset_postdata();
    ?>
    Thread Starter EnergyFreak

    (@energyfreak)

    As always, it works flawlessly. I will study the changes you made so I understand for next time. Thanks a lot! 🙂

    Thread Starter EnergyFreak

    (@energyfreak)

    Marking it as resolved.

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Add a second class to last post in loop’ is closed to new replies.