2017-03-22 10 views
1

私はWordpressのポストWordpressの - 最初の画像の後に表示コード

に私はXの段落の後にコードを置くための唯一の解決策を見つけたが、私は後にコードを置く必要がある最初の画像の後に、コードの表示ブロックのためのWordpressの機能を完了しようとしています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

関連する問題