Wordpress得到第一篇文章

时间:2014-09-14 02:07:27

标签: php wordpress

我正在尝试更改课程$firstPost = 0,但我不确定100%关于while loop等,因为我知道我不需要它

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 -->

1 个答案:

答案 0 :(得分:1)

最好为$firstPost使用布尔值。例如:

<?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;
?>