@charset "UTF-8";

/*!
Theme Name: Cocoon Child
Description: Cocoon専用の子テーマ
Theme URI: https://wp-cocoon.com/
Author: わいひら
Author URI: https://nelog.jp/
Template:   cocoon-master
Version:    1.1.3
*/

/************************************
** 子テーマ用のスタイルを書く
************************************/
/*必要ならばここにコードを書く*/

/************************************
** レスポンシブデザイン用のメディアクエリ
************************************/
/*1023px以下*/
@media screen and (max-width: 1023px){
  /*必要ならばここにコードを書く*/
}

/*834px以下*/
@media screen and (max-width: 834px){
  /*必要ならばここにコードを書く*/
}

/*480px以下*/
@media screen and (max-width: 480px){
  /*必要ならばここにコードを書く*/
}
<?php
/* ===========================================
 * H2ごとに本文を自動で背景帯でラップする
 * 既存記事・新規記事ともに有効
 * =========================================== */
add_filter('the_content', 'egix_wrap_sections_by_h2', 20);
function egix_wrap_sections_by_h2($content){
  // 管理画面/フィード/抜粋/AMPは除外
  if (is_admin() || is_feed()) return $content;
  if (function_exists('is_amp_endpoint') && is_amp_endpoint()) return $content;

  // 投稿と固定ページに適用（必要なら片方だけに）
  if (!(is_singular('post') || is_page())) return $content;

  // 記事ごとにオフにしたい場合：本文内に <!-- no-section-band --> があればスキップ
  if (strpos($content, '<!-- no-section-band -->') !== false) return $content;

  // すでに section-band が入っている（手動済み）記事は二重ラップ回避
  if (strpos($content, 'class="section-band') !== false) return $content;

  // H2で本文を分割（見出しタグ自体は保持）
  $parts = preg_split('/(<h2[^>]*>.*?<\/h2>)/is', $content, -1, PREG_SPLIT_DELIM_CAPTURE);
  if (!$parts || count($parts) < 2) return $content; // H2が無い

  $out = '';
  for ($i = 0; $i < count($parts); $i++){
    $chunk = $parts[$i];

    // 見出しでないプレーン部分（先頭の前書きなど）はそのまま
    if (!preg_match('/^<h2[^>]*>.*<\/h2>$/is', $chunk)){
      // 直前がH2なら、このプレーン部分を背景帯で包む
      if ($i > 0 && preg_match('/^<h2[^>]*>.*<\/h2>$/is', $parts[$i-1])) {
        // 空白だけの断片はスキップ
        if (trim(strip_tags($chunk)) === '') {
          $out .= $chunk;
        } else {
          $out .= '<div class="section-band gold">'.$chunk.'</div>';
        }
      } else {
        $out .= $chunk; // ただのテキスト（導入など）
      }
    } else {
      // H2（章タイトル）はそのまま出力
      $out .= $chunk;
    }
  }
  return $out;
}