Created
February 7, 2021 12:19
-
-
Save Hansanghyeon/73d8b0a388dc671432e37fb1cbace2e1 to your computer and use it in GitHub Desktop.
[wp] 현재 페이지에서 네비게이션 부모 쿼리 가져오기
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* 현재 페이지에서 네비게이션 부모 쿼리 가져오기 함수 | |
* | |
* @author Hansanghyeon | |
* @copyright Hansanghyeon <999@hyeon.pro> | |
**/ | |
function my_menu_parent($theme_location) | |
{ | |
global $post; | |
$locations = get_nav_menu_locations(); | |
if (isset($locations[$theme_location])) { | |
$menu = wp_get_nav_menu_object($locations[$theme_location]); | |
$menu_items = wp_get_nav_menu_items($menu->term_id); | |
_wp_menu_item_classes_by_context($menu_items); | |
$breadcrumbs = array(); | |
foreach ($menu_items as $menu_item) { | |
if ($menu_item->current_item_ancestor) { | |
$breadcrumbs[] = $menu_item->title; | |
break; | |
} | |
} | |
if (empty($breadcrumbs) && !empty($post)) { | |
foreach ($menu_items as $menu_item) { | |
if ($menu_item->object === $post->post_type) { | |
$breadcrumbs[] = get_the_title($menu_item->menu_item_parent); | |
break; | |
} | |
} | |
} | |
return $breadcrumbs; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment