2017-03-22 26 views
1

我想在WordPress的第一個圖像後完成的代碼顯示塊的WordPress功能後WordPress的 - 第一圖像之後顯示的代碼

我發現唯一的解決辦法X款後放碼,但我需要把後代碼X圖片

 <?php 
    $show_after_p = 2; 
    $content = apply_filters('the_content', $post->post_content); 
    if(substr_count($content, '<p>') > $show_after_p) 
    { 
     $contents = explode("</p>", $content); 
     $p_count = 1; 
     foreach($contents as $content) 
     { 
      echo $content; 
     if($p_count == $show_after_p) 
     { 
     ?> 
       YOUR AD CODE GOES HERE 
     <?php 
     } 
     echo "</p>"; 
     $p_count++; 
    } 
} 
?> 

回答

0

測試的解決方案。你可以放在functions.php或插件文件中。

function sr_display_code_after_image($content) 
{ 
    $show_after_img = 1; // Position of image after which you want to show. 
    if(substr_count($content, '<img') >= $show_after_img) 
    { 
     $contents = explode("<img", $content); 
     $img_count = 0; 
     foreach($contents as $content) 
     { 
      $before_tag = strstr($content, '/>', true); 
      $after_tag = strstr($content, '/>'); 
      echo "<img"; 
      echo $before_tag; 
      echo "/>"; 
      if($img_count == $show_after_img) 
      {   
       echo 'YOUR AD CODE GOES HERE'; 
      } 
      echo substr($after_tag, 2); 
      $img_count++; 
     } 
    } else 
    { 
     echo $content; 
    } 
} 
add_filter('the_content', 'sr_display_code_after_image'); 
+0

它的工作原理。謝謝:) – Marcin

+0

歡迎您:) – shazyriver