2016-08-24 36 views
0
 <?php 
     $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; 
     $args = array(
      'post_type' => 'post', 
      'posts_per_page' => 7, 
      'paged' => $paged); 
     $wp_query = new WP_Query($args); 
     $count = 0; 

     while (have_posts()) : the_post(); ?> 
      <?php echo get_the_title(); ?> 
      <?php 
       $count++; 
       if ($count === 1) { ?> 
        <div class="column small-12 margin-bottom-small"> 
         <div class="panel-brown"> 
          <img src="" alt="" /> 
          <h5 class="heading-white"><a href="#"><?php echo get_the_title(); ?></a></h5> 
          <div class="para-white"> 
           <?php $content = get_the_content(); echo mb_strimwidth($content, 0, 200, '...');?> 
          </div> 
         </div> 
        </div> 
      <?php } 
       while ($count <= 2) { 
        $count++ ?> 
        <div class="column small-12 medium-6 margin-bottom-small"> 
         <div class="panel-tan"> 
          <h5 class="heading-white"><a href="#"><?php echo get_the_title(); ?></a></h5> 
          <div> 
           <?php $content = get_the_content(); echo mb_strimwidth($content, 0, 200, '...');?> 
          </div> 
         </div> 
        </div> 
      <?php } 
       while ($count <= 4) { 
        $count++ ?> 
        <div class="column small-12 medium-6 margin-bottom-small"> 
         <div class="panel-tan"> 
          <h5 class="heading-white"><a href="#"><?php echo get_the_title(); ?></a></h5> 
          <div> 
           <?php $content = get_the_content(); echo mb_strimwidth($content, 0, 200, '...');?> 
          </div> 
         </div> 
        </div> 
      <?php } 
      ?> 
     <?php endwhile; ?> 

     <!-- Pagination links --> 
     <?php next_posts_link('&larr; Older posts', $wp_query ->max_num_pages); ?> 
     <?php previous_posts_link('Newer posts &rarr;'); ?> 

我掙扎理解爲什麼if和while語句循環相同的職位,但直接在此行之後:PHP - 同一職位而非顯示接下來的文章中

while (have_posts()) : the_post(); 

我在哪裏迴應標題,而是顯示帖子列表。

我想在這裏實現的是爲循環中輸出的每個帖子創建不同的html佈局。

乾杯

+0

你的意思是有第一個語句'如果$計數=== 1'但後來有'而$計數> = 2'?似乎這些都應該是如果陳述。我也看不出你是如何在主循環中使用WP_Query的結果......可能是手頭的問題 – Chizzle

回答

1

我認爲你是在你的查詢結果試圖環如圖所示WP_Query official documentation

此外,不知道爲什麼你會這樣做,而像你這樣的循環內主後循環本身,所以我改變了這些if語句。

像這樣的東西是什麼,我相信你是後:

<?php 
     $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; 
     $args = array(
      'post_type' => 'post', 
      'posts_per_page' => 7, 
      'paged' => $paged); 
     $wp_query = new WP_Query($args); 
     $count = 0; 

     if(! $wp_query->have_posts()) 
      die('no posts!'); // ADDED - Handle this more elegantly 

     while ($wp_query->have_posts()) : $wp_query->the_post(); // ADDED - Use query result object?> 
      <?php echo get_the_title(); ?> 
      <?php 
       $count++; 
       if ($count === 1) { ?> 
        <div class="column small-12 margin-bottom-small"> 
         <div class="panel-brown"> 
          <img src="" alt="" /> 
          <h5 class="heading-white"><a href="#"><?php echo get_the_title(); ?></a></h5> 
          <div class="para-white"> 
           <?php $content = get_the_content(); echo mb_strimwidth($content, 0, 200, '...');?> 
          </div> 
         </div> 
        </div> 
      <?php } 
       if ($count <= 2) { 
        ?> 
        <div class="column small-12 medium-6 margin-bottom-small"> 
         <div class="panel-tan"> 
          <h5 class="heading-white"><a href="#"><?php echo get_the_title(); ?></a></h5> 
          <div> 
           <?php $content = get_the_content(); echo mb_strimwidth($content, 0, 200, '...');?> 
          </div> 
         </div> 
        </div> 
      <?php } 
       if ($count <= 4) { // ADDED - Be careful, this will fire as well as the if statement above it since they are both <= 2 
        ?> 
        <div class="column small-12 medium-6 margin-bottom-small"> 
         <div class="panel-tan"> 
          <h5 class="heading-white"><a href="#"><?php echo get_the_title(); ?></a></h5> 
          <div> 
           <?php $content = get_the_content(); echo mb_strimwidth($content, 0, 200, '...');?> 
          </div> 
         </div> 
        </div> 
      <?php } 
      ?> 
     <?php endwhile; ?> 

     <!-- Pagination links --> 
     <?php next_posts_link('&larr; Older posts', $wp_query ->max_num_pages); ?> 
     <?php previous_posts_link('Newer posts &rarr;'); ?> 
+0

我試圖使用while循環,所以我不必有兩個if語句來減少我的代碼。讓我看看這個解決方案是否工作。謝謝 –

+0

對不起,這仍然輸出相同的帖子,而不是循環所有的帖子。 –

+0

啊我想通了。 –

0

這是不工作的原因是因爲我在while循環意味着它永遠不會是右邊數調用多個地方計數++ 。我通過在if和while循環之外放置一次來解決這個問題。

感謝你的努力的傢伙