상현
개발 덕후

현재페이지에서 메뉴항목 부모 메뉴 이름 가져오기

( 업데이트: )
<?php
function my_menu_parent($theme_location) {
  $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;
      }
    }
    return $breadcrumbs;
  }
 }Code language: HTML, XML (xml)

사용 방법


<?php
$parentitems = my_menu_parent('menu-1');
foreach ($parentitems as $parentitem) {
  echo $parentitem;
}Code language: HTML, XML (xml)