2017-06-27 127 views
0

不知何故在我的WordPress的自定義帖子 - 博客頁面我想能夠在第一篇文章中添加「選擇」類。WordPress的 - 如果第一篇文章添加選擇的類

什麼我做的是以下幾點:

    <?php if (have_posts()) : ?> 
        <?php $postcount = 0; ?> 
        <?php while (have_posts()) : the_post(); ?> 
        <?php $postcount++; ?> 

        <?php if ($postCount == 0) { ?> 
         <li class="selected" data-date="<?php the_time('F jS, Y'); ?>"> 
          <a class="news-box" href="<?php the_permalink(); ?>" target="_self"> 
           <?php the_post_thumbnail(); ?> 
           <div class="news-inner"> 
            <div class="news-inner-wrapper"> 
             <h4><?php the_title(); ?></h4> 
             <div class="read-more"><?php the_excerpt(__('Continue reading »','example')); ?></div> 
             <div class="news-inner-article-date"><small>By <?php the_author_link(); ?></small></div> 
            </div> 

           </div> 
          </a> 
         </li> 
        <?php } else { ?> 
         <li data-date="<?php the_time('F jS, Y'); ?>"> 
          <a class="news-box" href="<?php the_permalink(); ?>" target="_self"> 
           <?php the_post_thumbnail(); ?> 
           <div class="news-inner"> 
            <div class="news-inner-wrapper"> 
             <h4><?php the_title(); ?></h4> 
             <div class="read-more"><?php the_excerpt(__('Continue reading »','example')); ?></div> 
             <div class="news-inner-article-date"><small>By <?php the_author_link(); ?></small></div> 
            </div> 
           </div> 
          </a> 
         </li> 
        <?php } ?> 
        <?php endwhile; ?> 
        <?php endif; ?> 

但是這也適用「選擇」在所有李時珍類。

任何想法?

+0

有沒有需要打開和關閉PHP標籤隨處可見 –

+1

它不應該,因爲你甚至測試0之前增加$ postcount 1,你應該總是讓第二種情況。不幸的是,你的var名稱是錯誤的($ postcount/$ postCount),並在PHP undefined等於(不是嚴格意義上)0. – Kaddath

回答

-1

有很多重複出現的,只是爲了顯示類,如果$postcount == 0
此外,不應計算爲true,因爲你$postcount++爲時尚早。

試試這個:

<?php if (have_posts()) : ?> 
    <?php $postcount = 0; ?> 
    <?php while (have_posts()) : the_post(); ?> 

     <li class="class="<?php if($postcount == 0) { echo 'selected'; } ?>" data-date="<?php the_time('F jS, Y'); ?>"> 
      <a class="news-box" href="<?php the_permalink(); ?>" target="_self"> 
       <?php the_post_thumbnail(); ?> 
       <div class="news-inner"> 
        <div class="news-inner-wrapper"> 
         <h4><?php the_title(); ?></h4> 
         <div class="read-more"><?php the_excerpt(__('Continue reading »','example')); ?></div> 
         <div class="news-inner-article-date"><small>By <?php the_author_link(); ?></small></div> 
        </div> 

       </div> 
      </a> 
     </li> 

     <?php $postcount++; ?> 

    <?php endwhile; ?> 
<?php endif; ?> 
+0

downvoter,care to comment? – Alex

-1

即使世界無用的代碼存在。試試這個

<?php if (have_posts()) { 
    $postcount = 0; 
    while (have_posts()) : the_post(); 
     if ($postcount == 0) { ?> 
      <li class="selected" data-date="<?php the_time('F jS, Y'); ?>"> 
     <?php } else { ?> 
      <li data-date="<?php the_time('F jS, Y'); ?>"> 
     <?php } ?> 
     <a class="news-box" href="<?php the_permalink(); ?>" target="_self"> 
     <?php the_post_thumbnail(); ?> 
     <div class="news-inner"> 
     <div class="news-inner-wrapper"> 
     <h4><?php the_title(); ?></h4> 
     <div class="read-more"><?php the_excerpt(__('Continue reading »','example')); ?></div> 
     <div class="news-inner-article-date"><small>By <?php the_author_link(); ?></small></div> 
     </div> 

    </div> 
    </a> 
    </li> 
    <?php 
     $postcount++; 
     endwhile; 
    ?> 
+0

'$ postCount == 0'永遠不會計算爲真,因爲'$ postcount ++;'在if語句之前調用 – Alex

+0

您是對的,我修正了這個問題......代碼太多了 –

相關問題