2016-11-23 45 views
0

我的目標是在每四個帖子後打印一些額外的html。在WordPress循環中,如何檢查已循環的郵件數量?

什麼是錯用下面的代碼

<?php while (have_posts()) : the_post(); ?> 
    <?php wc_get_template_part('content', 'product'); ?> 
    <?php 
     $posts = $wp_query->post_count; 

     if ($posts == 4) { 
      echo '<div class="clearfix"></div>'; 
     } 
    ?> 
<?php endwhile; // end of the loop. ?> 
+0

的可能的複製[通過WordPress的帖子環,幷包裹在一個DIV每個X職位(http://stackoverflow.com/questions/28247770/loop- through-wordpress-posts-and-wrap-each-x-post-in-a-div) – bodi0

回答

6

考慮的The Loop的基本代碼,您可以包括計數器,增加它在每個迭代上,並檢查其零模塊四。

<?php if (have_posts()) : $count = 1; ?> 
    <?php while (have_posts()) : the_post();?>  
    <!-- do stuff ... --> 
    <?php if ($count % 4 == 0): ?> 
     <!-- extra stuff every four posts --> 
    <?php endif; ?> 
    <?php $count++; endwhile; ?> 
<?php endif; ?> 
2

試試這個

<?php 
$count = 1; 
while (have_posts()) : the_post(); 
    wc_get_template_part('content', 'product'); 
    if ($count%4==0) { 
     echo '<div class="clearfix"></div>'; 
    } 
    $count++; 
endwhile; ?> 
+0

謝謝,你有語法錯誤,第4行,其他明智的作品很棒。再次感謝。有 –

+0

嗎?之後留下的符號;只需要刪除那個 –

+0

是的,我糾正了這個問題,並且兩個解決方案都起作用。 你更好更簡單。 謝謝。 –

0

有可能是在我之前的答案中的錯誤:它呼應了「關」的標籤每隔4個帖子的結尾 - 但如果什麼文章計數是5?(或任何其他數不能被4整除當然需要解決這個問題可能會有所不同,從HTML到HTML?)

請看下面的例子以5:

--post 1--
--post 2--
--post 3--
--post 4--
--closing tag--
--post 5--
??????

您可以看到,在第5個帖子之後,循環未用閉合標籤關閉。

所以我會建議使用此代碼(即取的職位數或posts_per_page的帳戶數):我知道這個問題已經回答

<?php 
    // taking @Jordi Nebot's code from his answer - thank you 
    if (have_posts()) : $count = 1; 
?> 
    <?php while (have_posts()) : the_post();?>  
    <!-- do stuff ... --> 
    <?php 
     // the conditions in the if are triggered after every 4th post 
     // OR if all the posts have been echoed OR all the posts for 
     // this page have been echoed 
     if ($count % 4 == 0 || $wp_query->found_posts == $count || $wp_query->posts_per_page == $count): 
    ?> 
     <!-- extra stuff every four posts --> 
    <?php endif; ?> 
    <?php $count++; endwhile; ?> 
<?php endif; ?> 
+0

這可能是一個真正令人擔憂的問題(我有時候使用'open'布爾變量來檢查循環後是否打開了任何標籤),但在這種情況下,OP的回顯爲'

',所以沒有任何機會讓未封閉的標籤儘可能我明白... –

+0

你是對的,標籤是關閉的 - 這就是爲什麼我寫到「需要解決這個問題......」 - 我們不知道,HTML(網站佈局)是否需要最終的clearfix。如果沒有它,網站的外觀可能會分崩離析 - 或者根本不需要它。我認爲大部分時間在一些浮動元素之後需要clearfix - 並且沒有清理(「關閉浮動」)下一個元素可能不像預期的那樣工作。 –

4

,但我覺得有更清潔的方式做這件事。

您可以使用WP_Query對象中的幾個有用屬性,而不必添加自定義增量器。

<?php 
// Do not forget this part, required if you do not define a custom WP_Query 
global $wp_query; 

// Below you can find some useful properties in the WP_Query object 
// Use them to their full potential 

// Gets filled with the requested posts from the database. 
echo $wp_query->posts; 

// The number of posts being displayed. 
echo $wp_query->post_count; 

// The total number of posts found matching the current query parameters 
echo $wp_query->found_posts; 

// The total number of pages. Is the result of $found_posts/$posts_per_page 
echo $wp_query->max_num_pages; 

// Index of the post currently being displayed. (Can only be used within the loop) 
while(have_posts()) : the_post(); 
    echo $wp_query->current_post; 
endwhile; 

當使用$ current_post屬性時,您的循環看起來像這樣。

<?php global $wp_query; ?> 
<?php while (have_posts()) : the_post(); ?> 
    <?php wc_get_template_part('content', 'product'); ?> 
    <?php 
     if (($wp_query->current_post + 1) % 4 === 0) { 
      echo '<div class="clearfix"></div>'; 
     } 
    ?> 
<?php endwhile; // end of the loop. ?> 

我相信這是一個很好和乾淨的解決方案。

瞭解更多關於WP_Query對象的位置: https://codex.wordpress.org/Class_Reference/WP_Query