2015-02-23 31 views
0

我試圖在無限滾動的wordpress站點的每個第7個帖子之間插入谷歌廣告。我搜索並找到了結果,但這並不適合我,因爲我使用的WordPress主題並不複雜。我發現結果是index.php文件<?php endwhile; ?>後添加以下代碼在主頁和類別wordpress的每第7個帖子之間插入廣告

<?php $postnum++; if($postnum%5 == 0) { ?> YOUR AD CODE HERE <?php } ?>

,但在我的情況下,循環不同於一般wordpress主題不同。我所擁有的循環就是這樣,我想知道如何在我的情況下實現上述代碼。

循環代碼

static function loop($template) { 
     global $wp_query;   
     echo '<input type="hidden" id="query-' . $template . '" value="' . urlencode(json_encode($wp_query -> query)) . '" />'; 
     if (count($wp_query->posts) > 0) { 
      if(self::is_grid($template)){ 
?> 
       <div class="loop-container-view grid"> 
        <?php self::loop_switch($template , 1); ?> 
       </div> 
<?php 
      }else{ 
?> 
       <div class="loop-container-view list" id="contloop"> 
        <?php self::loop_switch($template , 0); ?> 
       </div> 
<?php 
      } 

      get_template_part('pagination'); 
     } else { 
      get_template_part('loop', '404'); 
     } 
    } 

然後調用這個代碼,我做到了一個短一點

static function get($post , $template = 'blog_page'){ 

     $meta = meta::get_meta($post -> ID , 'settings'); 
     if(isset($meta['safe'])){ 
      if(meta::logic($post , 'settings' , 'safe')){ 
       $classes = ' nsfw'; 
      }else{ 
       $classes = ' '; 
      } 
     }else{ 
      $classes = ' '; 
     } 
    ?> 
     <!-- post --> 
     <article id="post-<?php echo $post -> ID; ?>" <?php post_class('post ' . $classes , $post -> ID); ?>> 
     </article> 
    <?php 
    } 

循環切換功能

static function loop_switch($template = '' , $grid = 1) { 
     global $wp_query; 
     if (!empty($template)) { 
      $ajax = false; 
     } else { 
      $query = array(); 
      $template = isset($_POST['template']) && strlen($_POST['template']) ? $_POST['template'] : exit(); 
      $query = isset($_POST['query']) && !empty($_POST['query']) ? (array)json_decode(urldecode($_POST['query'])) : exit(); 
      $query['post_status'] = 'publish'; 
      $wp_query = new WP_Query($query); 
      $grid = isset($_POST['grid']) ? (int)$_POST['grid'] : 1; 
      $ajax = true; 
     } 

     $template = str_replace(array('_hot' , '_new' , '_like') , '' , $template); 

     if($grid == 1){ 
      $k = 1; 
      $i = 1; 
      $nr = $wp_query->post_count; 

      if (layout::get_length(0, $template) == 940) { 
       $div = 3; 
      } else { 
       $div = 2; 
      } 

      foreach ($wp_query->posts as $post) { 
       $wp_query->the_post(); 
       if ($i == 1) { 
        if (($nr - $k) < $div) { 
         $classes = 'class="last"'; 
        } else { 
         $classes = ''; 
        } 
        echo '<div ' . $classes . '>'; 
       } 

       self::grid($post, $template); 

       if ($i % $div == 0) { 
        echo '</div>'; 
        $i = 0; 
       } 
       $i++; 
       $k++; 
      } 

      if ($i > 1) { 
       echo '</div>'; 
      } 
     }else{ 
      foreach($wp_query->posts as $index => $post) { 
       $wp_query->the_post(); 
       if ($index > 0) { 
        ?><!--<p class="delimiter">&nbsp;</p>--><?php 
       } 

       self::get($post, $template); 
      } 
     } 

     if($ajax){ 
      exit(); 
     } 
    } 

,後在這裏稱爲前置式page.php文件

<?php 
         $wp_query = new WP_Query(array('page_id' => options::get_value('front_page' , 'page'))); 

         if($wp_query -> post_count > 0){ 
          foreach($wp_query -> posts as $post){ 
           $wp_query -> the_post(); 
           $post_id = $post -> ID; 
        ?> 
           <article id="post-<?php the_ID(); ?>" <?php post_class() ?>> 
            <header class="entry-header"> 
             <h1 class="entry-title"><?php the_title(); ?></h1> 
             <!-- post meta top --> 
             <?php 
              if(meta::logic($post , 'settings' , 'meta')){ 
               get_template_part('post-meta-top'); 
              } 
             ?> 
            </header> 
            <div class="entry-content"> 
             <?php 
              /* if show featured image */ 
              if(options::logic('blog_post' , 'show_featured')){ 
               if(has_post_thumbnail ($post -> ID)){ 
                $src = wp_get_attachment_image_src(get_post_thumbnail_id($post -> ID) , 'full'); 
             ?> 
                <div class="featimg circle"> 
                 <div class="img"> 
                  <?php 
                   ob_start(); 
                   ob_clean(); 
                   get_template_part('caption'); 
                   $caption = ob_get_clean(); 
                  ?> 
                  <a href="<?php echo $src[0]; ?>" title="<?php echo $caption; ?>" class="mosaic-overlay" rel="prettyPhoto-<?php echo $post -> ID; ?>">&nbsp;</a> 
                  <?php the_post_thumbnail('600x200'); ?> 
                  <?php 
                   if(strlen(trim($caption))){ 
                  ?> 
                    <p class="wp-caption-text"><?php echo $caption; ?></p> 
                  <?php 
                   } 
                  ?> 
                 </div> 
                </div> 
             <?php 
               } 
              } 
             ?> 
            </div> 

            <footer class="entry-footer"> 
             <div class="share"> 
              <?php get_template_part('social-sharing'); ?> 
             </div> 
             <div class="excerpt"> 
              <?php the_content(); ?> 
              <?php wp_link_pages(); ?> 
             </div> 
            </footer> 
           </article> 
      <?php 
          } 
         }else{ 
          /* not found page */ 
          get_template_part('loop' , '404'); 
         } 
+0

該位在哪裏'loop_switch'功能?是叫'get()'嗎? – RST 2015-02-23 18:40:38

+0

@RST我用你想要的細節編輯了我的問題。如果你這樣做,它會真的幫助我。 – 2015-02-23 19:30:49

回答

0

在front-page.php中試試這個。您可能需要調整它以在正確的帖子數量後添加。

if($wp_query -> post_count > 0){ 
    $postnum = 0; 

    foreach($wp_query -> posts as $post){ 
     $postnum++; 

     if($postnum%5 == 0) { 
      echo '<p> ---- insert ad here ---- </p>'; 
     } 

     $wp_query -> the_post(); 
     $post_id = $post -> ID; 
?> 

就應更換的代碼

   if($wp_query -> post_count > 0){ 
        foreach($wp_query -> posts as $post){ 
         $wp_query -> the_post(); 
         $post_id = $post -> ID; 
      ?> 
+0

感謝您的幫助,但我不知道在哪裏放置代碼,因爲我不知道編程,這就是爲什麼我使用現成的主題。你能幫我把代碼放在front-page.php嗎?我已將front-page.php的補碼複製到此小提琴中。 http://jsfiddle.net/p5rdLhyu/1/ – 2015-02-24 14:13:06

+0

我試圖複製你的代碼,並將它放在幾個位置,但它顯示錯誤。謝謝 – 2015-02-24 14:14:15

+0

我已經調整了答案,但如果您不熟悉代碼,最好請開發人員/程序員進行更改。 – RST 2015-02-24 16:05:18

相關問題