WordPress.org

Support

Support » How-To and Troubleshooting » Make first post different

Make first post different

  • Hi, I have this code setup to grab my blog posts. I want to have my first post styled different (HTML and CSS). Would I need to create a new WP_Query? I want to have 100% flexibility, so I’m guessing this would be best option? Could someone please tell me how to do this. Thanks.

    <?php $blog_query = new WP_Query('category_name=blog&posts_per_page=5');
      while ($blog_query->have_posts()) : $blog_query->the_post();?>
    
    <div <?php post_class() ?> id="post-<?php the_ID(); ?>">
    
    <h3><?php the_time('d/m/Y'); ?> <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h3>
    
    <?php the_excerpt() ?>
    
    </div><!--post-->
    
    <?php endwhile; ?>
Viewing 1 replies (of 1 total)
  • esmi

    @esmi

    Forum Moderator

    Would I need to create a new WP_Query?

    Nope. eg:

    <?php $c = 0; $blog_query = new WP_Query('category_name=blog&posts_per_page=5');
    while ($blog_query->have_posts()) : $blog_query->the_post();?>
    
    <div <?php if( $c ==0 ) post_class('special'); else post_class() ?> id="post-<?php the_ID(); ?>">
    
    <h3><?php the_time('d/m/Y'); ?> <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h3>
    
    <?php the_excerpt() ?>
    
    </div><!--post-->
    <?php $c++;?>
    <?php endwhile; ?>

    You can then expand this to use if( $c ==0 ) to output different markup etc. and use the class .special to style the first post differently to the rest.

Viewing 1 replies (of 1 total)
  • The topic ‘Make first post different’ is closed to new replies.