WordPress.org

Ready to get started?Download WordPress

Forums

Make first post different (2 posts)

  1. driz
    Member
    Posted 4 years ago #

    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; ?>
  2. esmi
    Theme Diva & Forum Moderator
    Posted 4 years ago #

    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.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.