2017-08-28 98 views
0

我想在3,5,10個帖子後在我的WordPress插件循環中插入AdSense代碼。這是我的代碼:插入AdSense WordPress循環

<?php 
if (have_posts()) : ?> 

    <?php 
    while (have_posts()) : the_post(); 

     get_template_part('/framework/templates/content-grid', get_post_format()); 

    endwhile; 

    else : 

     get_template_part('/framework/templates/content', 'none'); 

endif; ?> 

我怎麼能管理這個?

+1

請嘗試使用括號以避免混淆,並使代碼更具可讀性。在這種情況下可能會稍微有些不同,但是對於較大的腳本來說它有很大的不同。 – Peon

回答

0
You can achieve this by creating a custom counter. 

    <?php 
    $i = 1; 
     if (have_posts()) : ?> 
     <?php 
        while (have_posts()) : the_post(); 

    if($i==3||$i==5||$i==10){ 
     /*Adsence here*/ 
     } 
      get_template_part('/framework/templates/content-grid', get_post_format()); 

     $i++; 
      endwhile; 

      else : 

      get_template_part('/framework/templates/content', 'none'); 

       endif; ?> 
+0

非常感謝你! –