Xwriteのフッターにフローティングボタンを追加するカスタマイズ

未分類
この記事は約9分で読めます。
記事内に広告が含まれています。

Xwrite 4.0.7

はじめに

以下を実現します。

フローティングメニューを設置する場合、従来は「カスタムHTML」ブロックにHTMLを記述し、CSSと組み合わせて表示する方法が一般的です。

しかし、この方法では設置するたびにHTMLを手作業で記述し、ウィジェットエリアに配置しなければなりません。メニュー項目を追加・変更する場合もHTMLを編集する必要があり、管理が面倒です。

そこで、WordPress標準のメニュー機能を利用し、スマートフォン画面の下部にアイコン付きのフローティングメニューを表示できるようにします。

メニュー項目の追加・削除・並べ替えはWordPressの管理画面から行えるため、HTMLを毎回記述する必要がありません。

この記事では、この方法について説明します。

完成イメージ

メニュー
フローティングメニュー

実装手順

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

  • ラベル
    PHPを追加

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

    // メニュー位置を追加
    add_action('init', function () {
      register_nav_menus([
        'mobile_footer' => 'モバイルフッターメニュー',
      ]);
    });
    
    
    // モバイルフッターメニューのHTML出力をカスタマイズするWalkerクラス
    class Xwrite_Mobile_Footer_Menu_Walker extends Walker_Nav_Menu {
      public function start_el(&$output, $data_object, $depth = 0, $args = null, $current_object_id = 0) {
        $item = $data_object;
    
        // メニュー項目のCSSクラスを取得
        $classes = array_filter((array) $item->classes);
    
        // Font Awesomeのクラスを抽出
        $icon_classes = array_filter($classes, function ($class) {
          return preg_match('/^(fa-|fa$)/', $class);
        });
    
        // アイコン用クラスをメニュー項目用クラスから除外
        $menu_classes = array_diff($classes, $icon_classes);
        $class_names = implode(' ', $menu_classes);
    
        // メニュータイトルを取得
        $title = $item->title;
    
        // リンク属性を設定
        $attributes = [];
    
        if (!empty($item->attr_title)) {
          $attributes['title'] = $item->attr_title;
        }
    
        if (!empty($item->target)) {
          $attributes['target'] = $item->target;
        }
    
        if (!empty($item->xfn)) {
          $attributes['rel'] = $item->xfn;
        }
    
        if (!empty($item->url)) {
          $attributes['href'] = $item->url;
        }
    
        if (!empty($item->current)) {
          $attributes['aria-current'] = 'page';
        }
    
        // リンク属性をHTMLに変換
        $attribute_string = '';
    
        foreach ($attributes as $attr => $value) {
          if ($value !== '') {
            $attribute_string .= ' ' . esc_attr($attr) . '="' . esc_attr($value) . '"';
          }
        }
    
        // メニュー項目を出力
        $output .= '<li class="' . esc_attr($class_names) . '">';
        $output .= '<a' . $attribute_string . '>';
    
        // Font Awesomeアイコンを出力
        if (!empty($icon_classes)) {
          $output .= '<span class="mobile-footer-nav__icon ' . esc_attr(implode(' ', $icon_classes)) . '" aria-hidden="true"></span>';
        }
    
        // メニュータイトルを出力
        $output .= '<span class="mobile-footer-nav__title">' . wp_kses_post($title) . '</span>';
        $output .= '</a>';
      }
    }
    
    
    // フローティングメニューを追加
    add_action('wp_footer', function () {
      if (!has_nav_menu('mobile_footer')) {
        return;
      }
    
      wp_nav_menu([
        'theme_location'        => 'mobile_footer',
        'container'             => 'nav',
        'container_class'       => 'mobile-footer-nav',
        'container_aria_label'  => 'モバイルフッターメニュー',
        'menu_class'            => 'mobile-footer-nav__list',
        'items_wrap'            => '<ul class="%2$s">%3$s</ul>',
        'depth'                 => 1,
        'fallback_cb'           => false,
        'walker'                => new Xwrite_Mobile_Footer_Menu_Walker(),
      ]);
    }, 99);
  • ラベル
    CSSを追加

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

    /* カスタマイズ用CSSプロパティ */
    :root {
      --mobile-footer-nav-background: #f5a800;
      --mobile-footer-nav-border-color: rgba(255, 255, 255, .3);
      --mobile-footer-nav-color: #ffffff;
      --mobile-footer-nav-height: 60px;
      --mobile-footer-nav-icon-size: 22px;
      --mobile-footer-nav-text-size: 12px;
    }
    
    /* モバイルフッターメニュー */
    .mobile-footer-nav {
      display: none;
    }
    
    /* スマートフォン */
    @media (width <= 991px) {
    
      .mobile-footer-nav {
        background-color: var(--mobile-footer-nav-background);
        bottom: 0;
        display: block;
        left: 0;
        padding-bottom: env(safe-area-inset-bottom);
        position: fixed;
        width: 100%;
        z-index: 99999;
      }
    
      .mobile-footer-nav__list {
        display: flex;
        list-style: none;
        margin: 0;
        padding: 0;
    
        > li {
          border-right: 1px solid var(--mobile-footer-nav-border-color);
          flex: 1;
          margin: 0;
          min-width: 0;
          padding: 0;
    
          &:last-child {
            border-right: none;
          }
    
          > a {
            align-items: center;
            color: var(--mobile-footer-nav-color);
            display: flex;
            flex-direction: column;
            font-size: var(--mobile-footer-nav-text-size);
            justify-content: center;
            line-height: 1.4;
            min-height: var(--mobile-footer-nav-height);
            padding: 8px 2px;
            text-align: center;
            text-decoration: none;
          }
        }
      }
    
      .mobile-footer-nav__icon {
        color: var(--mobile-footer-nav-color);
        display: block;
        font-size: var(--mobile-footer-nav-icon-size);
        line-height: 1;
        margin-bottom: 4px;
      }
    
      .mobile-footer-nav__title {
        display: block;
      }
    
      /* メニューに隠れないように下部の余白を確保 */
      body:has(.mobile-footer-nav) {
        padding-bottom: calc(
          var(--mobile-footer-nav-height) + env(safe-area-inset-bottom)
        );
      }
    }

さいごに

今回紹介した方法を使えば、WordPress標準のメニュー機能を利用して、スマートフォン画面の下部にアイコン付きのフローティングメニューを設置できます。
メニュー項目の追加・削除・並べ替えは管理画面から行えるため、HTMLを直接編集する手間もありません。
また、Font AwesomeのアイコンやCSSカスタムプロパティを利用して、表示内容や色、サイズなども調整できます。
なお、このカスタマイズはXwriteに限らず、WordPressのメニュー機能を備えた他のテーマでも応用できます。
独自のフッターナビゲーションを設置したい場合に活用してみてください。

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