mogice
3/28/2017 - 1:53 AM

投稿本文下ウィジェットの追加

投稿本文下ウィジェットの追加

/* 投稿本文下ウィジェットのスタイル */
.widget-bottom-article {
  text-align: center;
}
///////////////////////////////////////
// 投稿本文下ウィジェットの追加
///////////////////////////////////////
register_sidebars(1,
  array(
  'name'=>'投稿本文下',
  'id' => 'widget-bottom-article',
  'description' => '投稿本文下に表示されるウイジェット。投稿本文の後に表示されます。',
  'before_widget' => '<div id="%1$s" class="widget-bottom-article %2$s">',
  'after_widget' => '</div>',
  'before_title' => '<div class="widget-bottom-article-title">',
  'after_title' => '</div>',
));

///////////////////////////////////////
// 投稿本文下にウィジェットを追加する処理
///////////////////////////////////////
add_filter( 'the_content', function ($the_content) {
  if ( is_single() && //投稿ページのとき、固定ページも表示する場合はis_singular()にする
       is_active_sidebar( 'widget-bottom-article' ) //ウィジェットが設定されているとき
  ) {
    //広告(AdSense)タグを記入
    ob_start();//バッファリング
    dynamic_sidebar( 'widget-bottom-article' );//本文下ウィジェットの表示
    $ad_template = ob_get_clean();
    $the_content .= $ad_template;
  }
  return $the_content;
});