doufan9805 2014-09-14 02:07
浏览 63
已采纳

Wordpress得到第一篇文章

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 -->
  • 写回答

1条回答 默认 最新

  • dongpanbo4727 2014-09-14 02:45
    关注

    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;
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?