Jess McKenzie
Jess McKenzie

Reputation: 8385

Wordpress getting first post

I am trying to change the class if $firstPost = 0but i'm not sure 100% about the while loop etc as I have an idea I don't need it

PHP/HTML

<?php
    $getPosts = new wp_query(array('showposts' => 5, 'orderby' => 1));


     if($getPosts->have_posts()):
      $firstPost = 0;

      while($getPosts->have_posts()):
        $getPosts->the_post();

      $cssClass = '';

      if($firstPost == 0)
      {
        $cssClass = array('article','first-post');

      }else{
        $cssClass = array('article');
      }
?>


<article <?php post_class($cssClass); ?> id="post-<?php the_ID(); ?>" itemscope itemtype="http://schema.org/Article">

    <?php if ( is_front_page() && is_home() ): ?>

        <div class="post-thumbnail">
            <?php if ( get_the_post_thumbnail($post_id) != '' ) { ?>
                <a href="<?php the_permalink(); ?>" class="thumbnail-wrapper"><?php the_post_thumbnail(); ?></a>
                <?php } else { ?>
                <a href="<?php the_permalink(); ?>" class="thumbnail-wrapper"><img src="<?php echo catch_that_image(); ?>" alt="" /></a>
            <?php } ?>
        </div>

        <article class="post-content">

                <header class="post-header">
                    <div class="post-category"><?php the_category( ', ' ) ?></div>
                    <h2 class="post-title" itemprop="name"><a href="<?php the_permalink(); ?>" rel="<?php esc_attr_e( 'bookmark','beautylust-theme' ); ?>"><?php the_title(); ?></a></h2>
                    <div class="post-date"><a href="<?php the_permalink(); ?>"><?php echo get_the_date( 'F j, Y' ) ?>.</a></div>
                    <div class="post-comment"><?php comments_popup_link( __( '0 comments','adelle-theme' ), __( '1 Comment','adelle-theme' ), __( '% Comments','adelle-theme' ) ); ?></div>
                </header>
                <?php the_excerpt(); ?>

                <footer class="post-footer"></footer><!-- .post-footer -->

        </article><!-- .post-content -->

    <?php endif; ?>

</article><!-- .article -->

Upvotes: 0

Views: 6543

Answers (1)

AlliterativeAlice
AlliterativeAlice

Reputation: 12577

It's better to use a boolean value for $firstPost. For example:

<?php
    $getPosts = new WP_Query(array('showposts' => 5, 'orderby' => 1));


     if($getPosts->have_posts()):
          $firstPost = true;

          while($getPosts->have_posts()):
                $getPosts->the_post();

              $cssClass = '';

              if($firstPost)
              {
                $cssClass = array('article','first-post');

              }else{
                $cssClass = array('article');
              }
?>


<article <?php post_class($cssClass); ?> id="post-<?php the_ID(); ?>" itemscope itemtype="http://schema.org/Article">

    <?php if ( is_front_page() && is_home() ): ?>

        <div class="post-thumbnail">
            <?php if ( get_the_post_thumbnail($post_id) != '' ) { ?>
                <a href="<?php the_permalink(); ?>" class="thumbnail-wrapper"><?php the_post_thumbnail(); ?></a>
                <?php } else { ?>
                <a href="<?php the_permalink(); ?>" class="thumbnail-wrapper"><img src="<?php echo catch_that_image(); ?>" alt="" /></a>
            <?php } ?>
        </div>

        <article class="post-content">

                <header class="post-header">
                    <div class="post-category"><?php the_category( ', ' ) ?></div>
                    <h2 class="post-title" itemprop="name"><a href="<?php the_permalink(); ?>" rel="<?php esc_attr_e( 'bookmark','beautylust-theme' ); ?>"><?php the_title(); ?></a></h2>
                    <div class="post-date"><a href="<?php the_permalink(); ?>"><?php echo get_the_date( 'F j, Y' ) ?>.</a></div>
                    <div class="post-comment"><?php comments_popup_link( __( '0 comments','adelle-theme' ), __( '1 Comment','adelle-theme' ), __( '% Comments','adelle-theme' ) ); ?></div>
                </header>
                <?php the_excerpt(); ?>

                <footer class="post-footer"></footer><!-- .post-footer -->

        </article><!-- .post-content -->

    <?php endif; ?>

</article><!-- .article -->

<?php
            $firstPost = false;
          endwhile;
    endif;
?>

Upvotes: 1

Related Questions