タイトルの通りです。
アドセンスコードの設定を行いたいと考えていますが、どうしたらよいでしょうか?
現在は以下のようにh2タグごとに広告を挿入するかたちで実装していますが、長いページの記事の一番下(具体的には、記事下まで2400px以上なら)に4番目のコードを挿入したいと思っております。
function add_ad_before_h2_for_3times($the_content) { //1つ目の広告タグを挿入(PC表示用) $ad1_pc = <<< EOF <script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> <!-- h2-1 --> <style type="text/css"> .adslot_1 { width: 300px; height: 250px; } @media (min-width:340px) { .adslot_1 { width: 336px; height: 280px; } } </style> <ins class="adsbygoogle adslot_1" style="display:inline-block;" data-ad-client="ca-pub-*****************" data-ad-slot="**************"></ins> <script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"> </script> <script>(adsbygoogle = window.adsbygoogle || []).push({});</script> EOF; //2つ目の広告タグを挿入(PC表示用) $ad2_pc = <<< EOF <script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> <!-- h2-2 --> <style type="text/css"> .adslot_1 { width: 300px; height: 250px; } @media (min-width:340px) { .adslot_1 { width: 336px; height: 280px; } } </style> <ins class="adsbygoogle adslot_1" style="display:inline-block;" data-ad-client="ca-pub-*****************" data-ad-slot="***************"></ins> <script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"> </script> <script>(adsbygoogle = window.adsbygoogle || []).push({});</script> EOF; //3つ目の広告タグを挿入(PC表示用) $ad3_pc = <<< EOF <script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> <!-- h2-3 --> <style type="text/css"> .adslot_1 { width: 300px; height: 250px; } @media (min-width:340px) { .adslot_1 { width: 336px; height: 280px; } } </style> <ins class="adsbygoogle adslot_1" style="display:inline-block;" data-ad-client="ca-pub-*****************" data-ad-slot="*******"></ins> <script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"> </script> <script>(adsbygoogle = window.adsbygoogle || []).push({});</script> EOF; if ( is_single() ) {//投稿ページ(PC) $h2 = '/^<h2.*?>.+?<\/h2>$/im';//H2見出しのパターン if ( preg_match_all( $h2, $the_content, $h2s )) {//H2見出しが本文中にあるかどうか if ( $h2s[0] ) {//チェックは不要と思うけど一応 if ( $h2s[0][0] ) {//1番目のH2見出し手前に広告を挿入 $the_content = str_replace($h2s[0][0], $ad1_pc.$h2s[0][0], $the_content); } if ( $h2s[0][1] ) {//2番目のH2見出し手前に広告を挿入 $the_content = str_replace($h2s[0][1], $ad2_pc.$h2s[0][1], $the_content); } if ( $h2s[0][2] ) {//3番目のH2見出し手前に広告を挿入 $the_content = str_replace($h2s[0][2], $ad3_pc.$h2s[0][2], $the_content); } } } } return $the_content; } add_filter('the_content','add_ad_before_h2_for_3times');
回答1件
あなたの回答
tips
プレビュー