From 1be14ea8837bda58c53bfbad41c448d4e601a246 Mon Sep 17 00:00:00 2001 From: kurudrive Date: Wed, 8 Jan 2025 22:18:30 +0900 Subject: [PATCH] =?UTF-8?q?=E3=82=AF=E3=83=A9=E3=82=B7=E3=83=83=E3=82=AF?= =?UTF-8?q?=E3=83=8A=E3=83=93=E3=82=82=E5=90=8C=E4=B8=80=E3=82=AF=E3=83=A9?= =?UTF-8?q?=E3=82=B9=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- inc/nav-menu-class-custom.php | 177 ++++++++++------------------------ 1 file changed, 52 insertions(+), 125 deletions(-) diff --git a/inc/nav-menu-class-custom.php b/inc/nav-menu-class-custom.php index bfb3befd..ef5e3e26 100644 --- a/inc/nav-menu-class-custom.php +++ b/inc/nav-menu-class-custom.php @@ -1,4 +1,10 @@ url ) ) { - $classes[] = 'current-menu-ancestor'; + $classes[] = $add_current_class_name; + } else { + // 投稿のトップのメニューアイテムは、カスタム投稿タイプのページを表示していても + // 勝手に current クラスが付与されるので、 + // アクティブでないと判断された項目に current クラスがあった場合は削除する必要がある + + // 現在配列に入っているclassをループ + for ( $i = 1; $i <= count( $classes ); $i++ ) { + if ( isset( $classes[ $i ] ) ) { + // currentがあった場合 + if ( $classes[ $i ] == $add_current_class_name || $classes[ $i ] == 'current_page_parent' ) { + // そのクラスをキーごと削除 + unset( $classes[ $i ] ); + } + } + } } + + // キーのフリなおし + $classes = array_values( $classes ); return $classes; } @@ -112,20 +139,32 @@ public static function get_post_type_from_url( $url ) { } /** - * カレントメニューアイテムかどうかを判定する + * Get displaying page post type slug * - * @param array $item_src : メニューアイテムの属性 - * @return bool : カレントメニューアイテムかどうか + * @return string : post type slug */ - public static function is_active_menu_item( $item_src ) { - $return = false; - $displaying_page_post_type_info = array(); + public static function get_displaying_page_post_type_slug() { // 今表示しているページが属する投稿タイプを取得 if ( function_exists( 'vk_get_post_type' ) ) { $displaying_page_post_type_info = vk_get_post_type(); + $displaying_page_post_type_slug = $displaying_page_post_type_info['slug']; } else { - $displaying_page_post_type_info['slug'] = get_post_type(); + $displaying_page_post_type_slug = get_post_type(); } + return $displaying_page_post_type_slug; + } + + /** + * カレントメニューアイテムかどうかを判定する + * + * @param array $item_src : メニューアイテムの属性 + * @return bool : カレントメニューアイテムかどうか + */ + public static function is_active_menu_item( $item_src ) { + $return = false; + + // 今表示しているページが属する投稿タイプを取得 + $displaying_page_post_type_slug = self::get_displaying_page_post_type_slug(); /* 投稿アーカイブの指定された固定ページメニューアイテムの処理 @@ -135,16 +174,17 @@ public static function is_active_menu_item( $item_src ) { // 投稿トップのメニューアイテム if ( $post_top_url === $item_src ) { - if ( $displaying_page_post_type_info['slug'] === 'post' ) { + if ( $displaying_page_post_type_slug === 'post' ) { // 今表示しているページの投稿タイプが post の場合 $return = true; } } $menu_url_post_type = self::get_post_type_from_url( $item_src ); - // 今表示しているページの投稿タイプとメニューに記入されているURLの投稿タイプが同じ場合 - if ( isset( $menu_url_post_type ) && isset( $displaying_page_post_type_info['slug'] ) ) { - if ( $displaying_page_post_type_info['slug'] === $menu_url_post_type ) { + + if ( ! empty( $menu_url_post_type ) && ! empty( $displaying_page_post_type_slug ) ) { + // 今表示しているページの投稿タイプとメニューに記入されているURLの投稿タイプが同じ場合 + if ( $displaying_page_post_type_slug === $menu_url_post_type ) { $return = true; } } @@ -156,116 +196,3 @@ public static function is_active_menu_item( $item_src ) { } VkNavMenuClassCustom::init(); - -add_filter( 'nav_menu_css_class', 'veu_add_current_class_to_classic_nav', 10, 2 ); -function veu_add_current_class_to_classic_nav( $classes, $item ) { - - // 今表示しているページが属する投稿タイプを取得 - if ( function_exists( 'vk_get_post_type' ) ) { - $post_type_info = vk_get_post_type(); - } else { - $post_type_info['slug'] = get_post_type(); - } - - // 付与するカレントクラス名 - $add_current_class_name = 'current-menu-ancestor'; - - /* - 投稿アーカイブの指定された固定ページメニューアイテムの処理 - /*-------------------------------------------*/ - $post_top_id = get_option( 'page_for_posts' ); - $post_top_url = get_the_permalink( $post_top_id ); - - if ( $post_top_url === $item->url ) { - if ( $post_type_info['slug'] === 'post' ) { - // 今表示しているページの投稿タイプが post の場合 - // currentクラスを付与 - $classes[] = $add_current_class_name; - } else { - // 今表示しているページの投稿タイプが post 以外の場合 - - // 現在配列に入っているclassをループ - for ( $i = 1; $i <= count( $classes ); $i++ ) { - if ( isset( $classes[ $i ] ) ) { - // currentがあった場合 - if ( $classes[ $i ] == $add_current_class_name || $classes[ $i ] == 'current_page_parent' ) { - // そのクラスをキーごと削除 - unset( $classes[ $i ] ); - } - } - } - - // キーのフリなおし - $classes = array_values( $classes ); - } - } - - /* - カスタムメニューに設定されたURLの投稿タイプ名を取得する - /*-------------------------------------------*/ - - // メニューがカスタムリンクでリンク先がカスタム投稿タイプのアーカイブの時 - - if ( $item->type == 'custom' || $item->type == 'post_type_archive' ) { - - // リライトルールを取得 - $rewrite_rules = get_option( 'rewrite_rules' ); - - if ( ! $rewrite_rules || ! is_array( $rewrite_rules ) ) { - - // リライトルールを無指定で使っている場合 - - $pattern = '/.*post_type=(.*)/'; - $subject = $item->url; - preg_match( $pattern, $subject, $matches ); - - // メニューの投稿タイプが取得できたら - if ( isset( $matches[1] ) ) { - $menu_url_post_type = $matches[1]; - } else { - $menu_url_post_type = ''; - }// if ( isset( $matches[1] ) ) { - } else { - - // リライトルールが普通に保存されている場合 - - // リライトルールをループ - foreach ( $rewrite_rules as $key => $value ) { - - // メニューに記載されているURLから投稿タイプ名を判別する - - // ループ中のりライトルールがメニューのURLと合致するか正規表現で検出 - $pattern = '{' . $key . '}'; - $subject = $item->url; - preg_match( $pattern, $subject, $matches ); - - // マッチした場合 - if ( $matches ) { - - // マッチした $value の URL ( index.php?post_type=custom など ) から投稿タイプが判別できる - // 正規表現で post_type= の値を抽出する - - $pattern = '/index.php\?post_type=(.*)/'; - $subject = $value; - preg_match( $pattern, $subject, $matches ); - - // メニューの投稿タイプが取得できたら - if ( isset( $matches[1] ) ) { - $menu_url_post_type = $matches[1]; - // 最初にマッチしてクラスを付与したら抜ける - break; - } // if ( isset( $matches[1] ) ) { - } // if ( $matches ) { - } // foreach ( $rewrite_rules as $key => $value ) { - } - - // 今表示しているページの投稿タイプとメニューに記入されているURLの投稿タイプが同じ場合 - if ( isset( $menu_url_post_type ) && isset( $post_type_info['slug'] ) ) { - if ( $post_type_info['slug'] === $menu_url_post_type ) { - $classes[] = $add_current_class_name; - } - } - } // if ( $item->object == 'custom' && $item->type == 'post_type_archive' ) { - - return $classes; -}