モーダルメニュー内にアコーディオンを配置する

Filed under: css,JavaScript — kdcs @ 2026年3月31日 火曜日

多くの選択肢をモーダルメニューに入れるとメニューが縦長になりすぎてしまうのでアコーディオン化して閉じておく処理

例は、ブランド一覧
html

<?php /* gn ブランド一覧ここから---------------------------------------------------------- */ ?>
<div class="gnBrandList">
<button class="gnBrandBtn">BRAND LIST</button>
<ul class="gnBrandMenu">
<li><a href="<?php echo home_url(); ?>/items_cat/cafering" title="CAFERING">CAFERING</a></li>
<li><a href="<?php echo home_url(); ?>/items_cat/cafering_k" title="CAFERING K">CAFERING K</a></li>
<li><a href="<?php echo home_url(); ?>/items_cat/hoshinosuna" title="HOSHInoSUNA">HOSHInoSUNA 星の砂</a></li>
<li><a href="<?php echo home_url(); ?>/items_cat/belethe" title="BELETHE">BELETHE</a></li>
<li><a href="<?php echo home_url(); ?>/items_cat/k_uno" title="disney-treasure">K.UNO "Disney Treasure"</a></li>
<li><a href="<?php echo home_url(); ?>/items_cat/sakura_diamond" title="さくらダイヤモンド">さくらダイヤモンド</a></li>
<li><a href="<?php echo home_url(); ?>/items_cat/fika" title="fika">fika</a></li>
<li><a href="<?php echo home_url(); ?>/items_cat/lapage" title="LAPAGE">LAPAGE</a></li>
<li><a href="<?php echo home_url(); ?>/items_cat/nina_ricci" title="NINA RICCI">NINA RICCI</a></li>
<li><a href="<?php echo home_url(); ?>/items_cat/private_beach" title="ハワイアンジュエリー「プライベートビーチ」">private beach</a></li>
<li><a href="<?php echo home_url(); ?>/items_cat/lor" title="L'or">L'or</a></li>
<li><a href="<?php echo home_url(); ?>/items_cat/ptau" title="ptau">ptau</a></li>
<li><a href="<?php echo home_url(); ?>/items_cat/truelove" title="True Love">True Love</a></li>
<li><a href="<?php echo home_url(); ?>/items_cat/fujita_original_selection" title="Fujita Original Selection">Fujita Original Selection</a></li>
</ul>
</div><!--/.gnBrandList-->
<?php /* gn ブランド一覧ここまで---------------------------------------------------------- */ ?>

css

/* ▼アコーディオン --------------------*/
.gnBrandList {
    width: 100%;
    margin-bottom: 20px;
}
/* 初期状態は閉じる */
.gnBrandMenu {
    max-height: 0;
    overflow: hidden;
    transition: max-height 0.3s ease;
    padding-left: 0;
    margin: 0;
}
.gnBrandMenu li {
    list-style: none;
}
.gnBrandMenu li a {
    display: block;
    padding: 8px 10px;
    border-bottom: 1px solid #ddd;
    text-decoration: none;
    color: #333;
}
.gnBrandBtn {
    width: 100%;
    padding: 10px;
    background: #937a6b;
    color: #fff;
    border: none;
    border-radius: 5px;
    cursor: pointer;
    font-size: 16px;
    position: relative;
}
/* ▼ の矢印を疑似要素で作る */
.gnBrandBtn::after {
    content: "▼";
    position: absolute;
    right: 15px;
    top: 50%;
    transform: translateY(-50%);
    transition: transform 0.3s ease;
}
/* 開いた時に矢印を回転 */
.gnBrandBtn.active::after {
    transform: translateY(-50%) rotate(180deg);
}
/* アコーディオン本体 */
.gnBrandMenu {
    max-height: 0;
    overflow: hidden;
    transition: max-height 0.6s ease;
    padding-left: 0;
    margin: 0;
}

javascript

// アコーディオン
document.querySelector('.gnBrandBtn').addEventListener('click', function() {
  const menu = document.querySelector('.gnBrandMenu');

  if (menu.classList.contains('open')) {
    // 閉じるとき
    menu.style.maxHeight = null;
  } else {
    // 開くとき:中身の高さを自動で取得
    menu.style.maxHeight = menu.scrollHeight + "px";
  }

  menu.classList.toggle('open');
  this.classList.toggle('active'); // ← 矢印回転
});

【重要】モーダルメニューを閉じるときにアコーディオンを閉じた状態にしたいのでモーダルメニュー開閉のjavascriptに「モーダル閉じたらアコーディオンも閉じる」を組み込んだコードにする必要がある。

こちらが組み込んだモーダルのjs

// 新スマホ グローバルメニュー -------------------------------------------------

var trigger = document.querySelector('.globalMenu');
var closeButtons = document.querySelectorAll('.closeButton');
var body = document.body;
var modal = document.getElementById('globalNavArea');
let scrollPosition = 0;

// ★ アコーディオンを閉じる処理(チラつき防止付き)
function resetAccordion() {
  const menu = document.querySelector('.gnBrandMenu');
  const btn = document.querySelector('.gnBrandBtn');

  if (!menu || !btn) return;

  // ★ transition を一時的に無効化(チラつき防止)
  menu.style.transition = 'none';

  // アコーディオンを閉じる
  menu.classList.remove('open');
  btn.classList.remove('active');
  menu.style.maxHeight = null;

  // ★ 次のフレームで transition を元に戻す
  requestAnimationFrame(() => {
    menu.style.transition = '';
  });
}

// モーダルを閉じる処理
function closeModal() {
  trigger.classList.remove('is-active');
  body.classList.remove('is-nav-opened');
  body.classList.add('is-nav-close');

  body.style.top = '';
  window.scrollTo(0, scrollPosition);

  // ★ モーダルを閉じたらアコーディオンも閉じる
  resetAccordion();
}

// モーダルを開く処理
function openModal() {
  scrollPosition = window.scrollY;

  trigger.classList.add('is-active');

  if (body.classList.contains('is-nav-close')) {
    body.classList.remove('is-nav-close');
  }

  body.classList.add('is-nav-opened');

  body.style.top = `-${scrollPosition}px`;
  modal.scrollTop = 0;
}

// トリガークリック
trigger.addEventListener('click', function () {
  if (body.classList.contains('is-nav-opened')) {
    closeModal();
  } else {
    openModal();
  }
});

// closeButton クリックでも閉じる
closeButtons.forEach(btn => {
  btn.addEventListener('click', closeModal);
});

スマホでモーダルメニューを開閉する時にアコーディオンをリセット(閉じ状態)する

Filed under: JavaScript — kdcs @ 2026年3月31日 火曜日

モーダルメニューの中でアコーディオンを使う時、アコーディオンを開いた状態でメニューを閉じて再度モーダルメニューを開いたときにアコーディオンを閉じた状態にしたいので、モーダルメニュー開閉にアコーディオンのリセットを組み込んだコード。

var trigger = document.querySelector('.globalMenu');
var closeButtons = document.querySelectorAll('.closeButton');
var body = document.body;
var modal = document.getElementById('globalNavArea');
let scrollPosition = 0;

// ★ アコーディオンを閉じる処理(チラつき防止付き)
function resetAccordion() {
  const menu = document.querySelector('.gnBrandMenu');
  const btn = document.querySelector('.gnBrandBtn');

  if (!menu || !btn) return;

  // ★ transition を一時的に無効化(チラつき防止)
  menu.style.transition = 'none';

  // アコーディオンを閉じる
  menu.classList.remove('open');
  btn.classList.remove('active');
  menu.style.maxHeight = null;

  // ★ 次のフレームで transition を元に戻す
  requestAnimationFrame(() => {
    menu.style.transition = '';
  });
}

// モーダルを閉じる処理
function closeModal() {
  trigger.classList.remove('is-active');
  body.classList.remove('is-nav-opened');
  body.classList.add('is-nav-close');

  body.style.top = '';
  window.scrollTo(0, scrollPosition);

  // ★ モーダルを閉じたらアコーディオンも閉じる(チラつきなし)
  resetAccordion();
}

// モーダルを開く処理
function openModal() {
  scrollPosition = window.scrollY;

  trigger.classList.add('is-active');

  if (body.classList.contains('is-nav-close')) {
    body.classList.remove('is-nav-close');
  }

  body.classList.add('is-nav-opened');

  body.style.top = `-${scrollPosition}px`;
  modal.scrollTop = 0;
}

// トリガークリック
trigger.addEventListener('click', function () {
  if (body.classList.contains('is-nav-opened')) {
    closeModal();
  } else {
    openModal();
  }
});

// closeButton クリックでも閉じる
closeButtons.forEach(btn => {
  btn.addEventListener('click', closeModal);
});

アコーディオンをモーダルメニューの中で使わないときのコード

var trigger = document.querySelector('.globalMenu');
var closeButtons = document.querySelectorAll('.closeButton');
var body = document.body;
var modal = document.getElementById('globalNavArea');
let scrollPosition = 0;

// モーダルを閉じる処理
function closeModal() {
  trigger.classList.remove('is-active');
  body.classList.remove('is-nav-opened');
  body.classList.add('is-nav-close');

  body.style.top = '';
  window.scrollTo(0, scrollPosition);
}

// モーダルを開く処理
function openModal() {
  scrollPosition = window.scrollY;

  trigger.classList.add('is-active');

  if (body.classList.contains('is-nav-close')) {
    body.classList.remove('is-nav-close');
  }

  body.classList.add('is-nav-opened');

  body.style.top = `-${scrollPosition}px`;
  modal.scrollTop = 0;
}

// トリガークリック
trigger.addEventListener('click', function () {
  if (body.classList.contains('is-nav-opened')) {
    closeModal();
  } else {
    openModal();
  }
});

// closeButton クリックでも閉じる
closeButtons.forEach(btn => {
  btn.addEventListener('click', closeModal);
});

WordPressのjQuery出力のコントロール(wp_footerで出す)

Filed under: functions.php,wordpress — kdcs @ 2026年3月28日 土曜日

WordPressのデフォルトでは「wp_head」にjQueryが出力されるので基本的にはこの出力をfunctions.phpへの記述で停止して、footer.phpに直接読み込みを書いている。
しかし、状況によってはWordPressがjQueryの存在を認識できず、不具合が発生することがあるので、WordPressに認識させつつwp_footerで出力させる方法。

まず、現行のwp_headでjQueryの読み込みを停止する記述

// wp_head関連 wp_headでjQueryを読み込ませない--------------------------------------------
function my_delete_local_jquery() {
if (!is_admin()){
    wp_deregister_script('jquery');
  }
}
add_action( 'wp_enqueue_scripts', 'my_delete_local_jquery' );

wp_footerでjQueryを読み込ませる記述(テーマのjsフォルダ内にjquery.min.jsが必要)

// jQueryの出力(■注意!wp本体のjQueryを使わず直接読み込んでいる場合のみこの記述を使う)-
function register_custom_jquery() {

    // WordPress に「jquery」という名前で登録する
    wp_deregister_script('jquery'); // WP の jQuery を解除
    wp_register_script(
        'jquery',
        get_template_directory_uri() . '/js/jquery.min.js?v=3.7.1',
        array(),
        null,
        true
    );

    wp_enqueue_script('jquery'); // これでフッターに出力される
}
add_action('wp_enqueue_scripts', 'register_custom_jquery', 20);

WordPress「Favorites」プラグイン「追加・削除メッセージ」

Filed under: wordpressプラグイン — kdcs @ 2026年3月18日 水曜日

お気に入りボタンで、お気に入りに追加した時に「お気に入りに追加」、お気に入りから外した時に「お気に入りを削除」といったメッセージを画面に出す方法。

メッセージはcssで画面下部に設定、初期値display:noneで非表示。javascriptのjQuery「.fadeIn()」でdisplay: blockになり表示する。(表示時間をセットして、しばらく表示したら消えるという動き)
html

<div id="favoriteList"><div class="favoriteBox"><span class="favoriteMessage"></span><p class="favoriteText"><a href="<?php echo home_url(); ?>/favorite">お気に入りリストを見る</a></p></div></div>

javascript

$(function () {
  var timer = false;

  // Favorites 更新後イベント
  $(document).on('favorites-updated-single', function(event, favorites, post_id, site_id, status, button){

    // status は 'active' or 'inactive'
    if (status === 'active') {
      $('.favoriteMessage').text('アイテムを追加しました');
    } else {
      $('.favoriteMessage').text('アイテムを削除しました');
    }

    // 表示処理
    $('#favoriteList').fadeIn();

    if (timer !== false) {
      clearTimeout(timer);
    }

    timer = setTimeout(function () {
      $('#favoriteList').fadeOut();
    }, 5000);
  });
});

css

#favoriteList {
    display: none;
    width: 100%;
    position: fixed;
    bottom: 0;
    background: #fff;
    padding: 14px 0;
    opacity: 0.9;
    z-index: 100;
}

WordPress「Favorites」プラグイン2.3.6での設定

Filed under: wordpressプラグイン — kdcs @ 2026年3月17日 火曜日

お気に入りを付けるプラグイン「Favorites」のバージョン2.3.3では、表示設定でカスタムマークアップを選択し、「お気に入り未設定・お気に入り済み」をそれぞれボタンのマークアップhtmlで設定できた。(但し、実際にはfooter.phpに直接javascriptを記述したのでここでの設定は無関係になる)

Favorites2.3.3での設定記述
◆Button Markup: Unfavorited(お気に入りされていない)

<span>お気に入り</span><i class="fa icon-heart-empty"></i>

◆Button Markup: Favorited(お気に入り済み)

<span>お気に入り</span><i class="fa icon-heart"></i>

Favorites2.3.6では、設定記述を行うとちゃんと入力されない
そこで、そもそもここの設定記述は無関係なので空白にしておく
※何も入力しないと何故か上手く設定が保存できないので、それぞれに半角スペースを入れて保存する

WordPress 6.9.4とFavorites2.3.6での検証では2.3.3から行っているfooterの記述でそのまま行ける事を確認済み
※このfooter.phpへの記述にオプション設定が含まれる(ハートの色付けのcssも)

footer.php

<?php /* お気に入りオプション設定・スクリプトここから------------------------------------- */ ?>
<script>
var favorites_data = {"ajaxurl":"<?php echo home_url(); ?>/wp-admin/admin-ajax.php","nonce":"cb6b8ba573","favorite":"<span>\u304a\u6c17\u306b\u5165\u308a<\/span><i class=\"fa icon-heart-empty\"><\/i>","favorited":"<span>\u304a\u6c17\u306b\u5165\u308a<\/span><i class=\"fa icon-heart\"><\/i>","includecount":"","indicate_loading":"","loading_text":"Loading","loading_image":"","loading_image_active":"","loading_image_preload":"","cache_enabled":"1","button_options":{"button_type":"custom","custom_colors":true,"box_shadow":false,"include_count":false,"default":{"background_default":false,"border_default":false,"text_default":false,"icon_default":"#666666","count_default":false},"active":{"background_active":false,"border_active":false,"text_active":false,"icon_active":"#dd3333","count_active":false}},"authentication_modal_content":"<p>Please login to add favorites.<\/p><p><a href=\"#\" data-favorites-modal-close>Dismiss this notice<\/a><\/p>","authentication_redirect":"","dev_mode":"","logged_in":"1","user_id":"1","authentication_redirect_url":"https:\/\/localhost\/wp540-bjfx\/wp-login.php"};
</script>
<script src='<?php echo home_url(); ?>/wp-content/plugins/favorites/assets/js/favorites.min.js?ver=2.3.2'></script>
<?php /* お気に入りオプション設定・スクリプトここまで------------------------------------- */ ?>

サイト内検索

カテゴリー

最近の投稿

↑上に戻る