PC画面にハンバーガーメニューを追加する方法

Cocoonカスタマイズ集
この記事は約10分で読めます。
記事内に広告が含まれています。

完成イメージ

本ページに完成イメージを反映させます。

実装手順

以下の手順で実装します。

  • ラベル
    PHPを追加

    以下のコードをfunctions.phpに追加します。

    add_action('cocoon_part_after__tmp-user/body-top-insert', function () {
      ?>
      <div class="hamburger-menu">
        <!-- メニュー開閉状態を管理するチェックボックス(CSS制御用・非表示・アクセシビリティ対応) -->
        <input type="checkbox" id="hamburger-menu-check" class="hamburger-menu__check" aria-label="メニューを開閉する">
    
        <!-- ハンバーガーアイコンのラベル(クリックで開閉切り替え・アクセシビリティ対応) -->
        <label for="hamburger-menu-check" class="hamburger-menu__button" aria-label="メニュー">
          <span aria-hidden="true"></span>
          <span aria-hidden="true"></span>
          <span aria-hidden="true"></span>
        </label>
    
        <!-- 開閉式メニューの中身(サンプルリスト) -->
        <div class="hamburger-menu__content">
          <ul>
            <li><a href="#">menu1</a></li>
            <li><a href="#">menu2</a></li>
            <li><a href="#">menu3</a></li>
            <li><a href="#">menu4</a></li>
          </ul>
        </div>
      </div>
      <?php
    });
  • ラベル
    CSSを追加

    以下のコードをstyle.cssに追加します。

    .hamburger-menu {
      /* メニューボタン */
      .hamburger-menu__check {
        display: none;
      }
    
      .hamburger-menu__button {
        cursor: pointer;
        height: 50px;
        position: fixed;
        right: 30px;
        top: 10px;
        width: 60px;
        z-index: 9999;
    
        span {
          background-color: #333;
          border-radius: 2px;
          display: inline-block;
          height: 2px;
          left: 14px;
          position: absolute;
          transition: all 0.3s;
          width: 50%;
    
          &:nth-of-type(1) {
            top: 15px;
          }
    
          &:nth-of-type(2) {
            top: 23px;
          }
    
          &:nth-of-type(3) {
            top: 31px;
          }
        }
      }
    
      /* メニュー本体 */
      .hamburger-menu__content {
        background: rgb(0 0 0 / 50%);
        height: 100vh;
        left: 0;
        opacity: 0;
        position: fixed;
        top: 0;
        transition: all 0.3s;
        width: 100%;
        z-index: -1;
    
        ul {
          height: fit-content;
          inset: 0;
          margin: auto;
          padding: 0;
          position: fixed;
          z-index: 999;
        }
    
        li {
          display: block;
          list-style: none;
          text-align: center;
    
          a {
            color: #fff;
            padding: 10px;
            text-decoration: none;
          }
        }
      }
    
      /* チェック状態のスタイル変化 */
      &:has(.hamburger-menu__check:checked) {
        .hamburger-menu__content {
          opacity: 1;
          z-index: 999;
        }
    
        .hamburger-menu__button span {
          &:nth-of-type(1) {
            left: 16px;
            top: 18px;
            transform: translateY(6px) rotate(-45deg);
            width: 50%;
          }
    
          &:nth-of-type(2) {
            opacity: 0;
          }
    
          &:nth-of-type(3) {
            left: 16px;
            top: 30px;
            transform: translateY(-6px) rotate(45deg);
            width: 50%;
          }
        }
      }
    }
    
    @media (max-width: 1023px) {
      .hamburger-menu {
        display: none;
      }
    }

参考:メニューによる実装方法

新しいメニュー位置を作成せず、既存の未使用メニュー位置を代用します。
例えば、ヘッダーモバイルメニューを代用します。

実装手順

以下の手順で実装します。

  • ラベル
    メニューでの設定
    メニュー
    1. 外観→メニューでメニューを作成します。
    2. メニューの位置として、未設定の表示位置(例:「ヘッダーモバイルメニュー」)を選択します。
  • ラベル
    PHPを追加

    以下のコードをfunctions.phpに追加します。

    add_action('cocoon_part_after__tmp-user/body-top-insert', function () {
      ?>
      <div class="hamburger-menu">
        <!-- メニュー開閉状態を管理するチェックボックス(CSS制御用・非表示) -->
        <input type="checkbox" id="hamburger-menu-check" class="hamburger-menu__check">
    
        <!-- ハンバーガーアイコンのラベル(クリックでチェックボックスのオンオフを切り替え) -->
        <label for="hamburger-menu-check" class="hamburger-menu__button">
          <span></span>
          <span></span>
          <span></span>
        </label>
    
        <!-- 開閉式メニューの中身(コンテンツ部分) -->
        <div class="hamburger-menu__content">
          <?php
          // ヘッダーモバイルメニューを動的に出力
          wp_nav_menu([
            'theme_location' => NAV_MENU_HEADER_MOBILE,
            'container'      => false,
            'fallback_cb'    => false,
          ]);
          ?>
        </div>
      </div>
      <?php
    });

参考:patternショートコードによる実装方法

パターンを使ってメニューを追加します。
この際、「ナビゲーション」ブロックを利用すると、メニューの開閉などもCSS使わず、エディター上の操作だけで簡単に設定できます。

実装手順

以下の手順で実装します。

  • ラベル
    Gutenbergエディターでの設定
    Gutenbergエディター

    管理メニュー→パターン一覧からGutenbergエディターを使い、メニューを作成します。

  • ラベル
    PHPを追加

    以下のコードをfunctions.phpに追加します。

    add_action('cocoon_part_after__tmp-user/body-top-insert', function () {
      echo do_shortcode('[pattern id=パターンID]');
    });

さいごに

今回紹介した方法を使えば、CocoonにおいてPC表示時にハンバーガーメニューを簡単に追加できます。
これにより、デザインの幅が広がり、ユーザーにとっても使いやすいサイトを構築できるでしょう。

タイトルとURLをコピーしました