固定ページ一覧を自動でカード表示する方法

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

はじめに

以前の記事では、固定ページの子ページを表示する方法を紹介しました。

今回は、固定ページを自動取得し、Cocoonのブログカード形式で一覧表示するショートコードを追加します。

親ページと子ページ、孫ページなどの親子関係をもとに階層構造で表示し、子ページを持つ項目には開閉ボタンを設置します。
また、現在表示しているページを強調表示し、その親階層を自動的に展開することもできます。

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

完成イメージ

使用方法

以下にショートコードを示します。

[navi_list source="page"]

実装手順

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

  • ラベル
  • ラベル
    PHPを追加(固定ページ一覧)

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

    add_filter('pre_do_shortcode_tag', function($output, $tag, $attr, $m) {
      if ($tag !== 'navi_list') {
        return $output;
      }
    
      // source=category以外は処理対象外
      if (!is_array($attr) || ($attr['source'] ?? '') !== 'category') {
        return $output;
      }
    
      // ショートコードの属性を設定
      $atts = shortcode_atts([
        'hierarchy' => '',
      ], $attr);
    
      // カテゴリーを取得
      $categories = get_categories([
        'taxonomy'   => 'category',
        'hide_empty' => false,
        'orderby'    => 'name',
        'order'      => 'ASC',
      ]);
    
      // カテゴリーがない場合は終了
      if (!$categories) {
        return '';
      }
    
      // 最上位の親カテゴリーのみ取得
      if ($atts['hierarchy'] === 'parent') {
        $categories = array_filter($categories, function($category) {
          return (int) $category->parent === 0;
        });
      }
    
      // カードHTMLを初期化
      $cards = '';
    
      // カテゴリーごとにカードを生成
      foreach ($categories as $category) {
        // カテゴリーをメニュー項目形式に変換
        $menu = (object) [
          'ID'               => $category->term_id,
          'object'           => 'category',
          'object_id'        => $category->term_id,
          'menu_item_parent' => $category->parent,
          'url'              => get_category_link($category->term_id),
          'title'            => $category->name,
          'description'      => '',
          'classes'          => [],
          'target'           => '',
          'attr_title'       => '',
        ];
    
        // カード表示用の属性を設定
        $card_atts = [
          'prefix'           => WIDGET_NAVI_ENTRY_CARD_PREFIX,
          'url'              => $menu->url,
          'title'            => escape_shortcodes($menu->title),
          'snippet'          => '',
          'image_attributes' => get_navi_card_image_attributes($menu, 0),
          'ribbon_no'        => '',
          'type'             => 0,
          'classes'          => [],
          'object'           => 'category',
          'object_id'        => $category->term_id,
          'horizontal'       => 0,
          'target'           => '',
        ];
    
        // Cocoon標準のナビカードを取得
        $card = get_widget_entry_card_link_tag($card_atts);
    
        // 階層情報とカレント情報をカードリンクに追加
        $card = str_replace(
          '<a ',
          '<a data-menu-id="' . esc_attr($category->term_id) . '" data-menu-parent="' . esc_attr($category->parent) . '" data-menu-current="' . (is_category($category->term_id) ? '1' : '0') . '" ',
          $card
        );
    
        // カードを追加
        $cards .= $card;
      }
    
      // Cocoon標準のナビカード外枠を設定
      $wrap_atts = [
        'tag'        => $cards,
        'type'       => 0,
        'bold'       => 1,
        'arrow'      => 0,
        'class'      => '',
        'horizontal' => 0,
      ];
    
      // Cocoon標準のナビカード外枠を取得
      $html = get_navi_card_wrap_tag($wrap_atts);
    
      // 階層用クラスを追加
      return str_replace(
        'navi-entry-cards',
        'navi-entry-cards my-navi-hierarchy',
        $html
      );
    }, 10, 4);

さいごに

今回紹介した方法を使えば、固定ページを自動取得し、Cocoonのブログカード形式で親子階層に沿って一覧表示できます。
子ページは折りたたんで表示し、開閉ボタンから展開できます。また、現在表示しているページを強調表示し、その親階層を自動的に開くことも可能です。
固定ページの追加や階層変更にも自動で対応するため、一覧を手動で更新する手間を省けます。

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