Skip to content

Instantly share code, notes, and snippets.

@woodroots
Created March 30, 2013 21:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save woodroots/5278411 to your computer and use it in GitHub Desktop.
Save woodroots/5278411 to your computer and use it in GitHub Desktop.
本文中の1つ目の見出しの上に広告を挿入する
function content_ad($content){
//個別記事ページかどうか判定
if(is_single()){
//ヒアドキュメントで広告用コードの定義
$ad = <<< EOF
<div class="module">
<script type="text/javascript"><!--
google_ad_client = "ca-pub-0596916800718129";
/* wrbt */
google_ad_slot = "9046521069";
google_ad_width = 336;
google_ad_height = 280;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</div>
EOF;
//第4引数に何回置換するかを指定することができる。
$content = preg_replace('/(<h3>|<h4>)/',"$ad$1",$content,1);
return $content;
}
return $content;
}
add_filter('the_content','content_ad');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment