• Hi there,

    I’m trying to add a class to the first post in my loop. Here is my index.php –

    <main id="main" class="site-main frame" role="main">
    
          <?php $c = 0; $class = ''; ?>  
    
    		<?php if ( have_posts() ) : ?>
    
    	        <?php /* Start the Loop */ ?>
    		<?php while ( have_posts() ) : the_post(); ?>
    
                    <?php //set count
    			   $c++;
    			   if ( $c == 1 ) $class .= 'firstItem';
    			   if ( $c !== 1 ) $class .= '';
    		?>
    
    		<?php get_template_part( 'content', get_post_format() ); ?>
    
    		<?php endwhile; ?>
    
    		<?php byyd_paging_nav(); ?>
    
    		<?php else : ?>
    
    			<?php get_template_part( 'content', 'none' ); ?>
    
    	<?php endif; ?>
    </main><!-- #main -->

    And here is my content.php –

    <article id="post-<?php the_ID(); ?>" <?php post_class( "$class masonryItem" ); ?> onClick="window.open('<?php echo get_permalink(); ?>','_self');">
    *MY CONTENT*
    </article>

    I’ve tried various options but am unable to get this right… Any ideas?

    Thanks

Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
  • The topic ‘Adding class to first post not working’ is closed to new replies.