makeBCT

#AI #OpenAI #Chatbot #Cloud #WordPress

Showing the current page’s parent’s sub-menu items from a custom nav menu in WordPress

기타
Author
북극海
Date
2016-09-21 00:28
Views
3089

Showing the current page’s parent’s sub-menu items from a custom nav menu in WordPress

링크: http://wordpress.stackexchange.com/questions/32274/display-only-page-specific-sub-menu-items-using-custom-walker

functions.php 파일에 다음 코드 추가

class Custom_Walker_Nav_Sub_Menu extends Walker_Nav_Menu {

var $found_parents = array();

function start_el(&$output, $item, $depth, $args) {

global $wp_query;

//this only works for second level sub navigations
$parent_item_id = 0;

$indent = ( $depth ) ? str_repeat( "t", $depth ) : '';

$class_names = $value = '';

$classes = empty( $item->classes ) ? array() : (array) $item->classes;

$class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item ) );
$class_names = ' class="' . esc_attr( $class_names ) . '"';


if ( ($item->menu_item_parent==0) && (strpos($class_names, 'current-menu-parent')) ) {

$output.= '

<li>';

}


// Checks if the current element is in the current selection

if (strpos($class_names, 'current-menu-item')

|| strpos($class_names, 'current-menu-parent')

|| strpos($class_names, 'current-menu-ancestor')

|| (is_array($this->found_parents) && in_array( $item->menu_item_parent, $this->found_parents )) ) {

// Keep track of all selected parents
$this->found_parents[] = $item->ID;

//check if the item_parent matches the current item_parent
if($item->menu_item_parent!=$parent_item_id){

$output .= $indent . '<li id="menu-item-'. $item->ID . '"' . $value . $class_names .'>';

$attributes = ! empty( $item->attr_title ) ? ' title="' . esc_attr( $item->attr_title ) .'"' : '';
$attributes .= ! empty( $item->target ) ? ' target="' . esc_attr( $item->target ) .'"' : '';

$attributes .= ! empty( $item->xfn ) ? ' rel="' . esc_attr( $item->xfn ) .'"' : '';

$attributes .= ! empty( $item->url ) ? ' href="' . esc_attr( $item->url ) .'"' : '';

$item_output = $args->before;
$item_output .= '<a'. $attributes .'>';

$item_output .= $args->link_before . apply_filters( 'the_title', $item->title, $item->ID ) . $args->link_after;

$item_output .= '</a>';

$item_output .= $args->after;

$output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
}


}

}

function end_el(&$output, $item, $depth) {
// Closes only the opened li

if ( is_array($this->found_parents) && in_array( $item->ID, $this->found_parents ) ) {

$output .= "</li>n";

}

}

function end_lvl(&$output, $depth) {
$indent = str_repeat("t", $depth);

// If the sub-menu is empty, strip the opening tag, else closes it

if (substr($output, -22)=="<ul class="sub-menu">n") {

$output = substr($output, 0, strlen($output)-23);

} else {

$output .= "$indent</ul>n";

}

}

}

 

sidebar.php 에 다음 코드 추가

$menu_args = array(
'walker' => new Custom_Walker_Nav_Sub_Menu(),

'container' => '',

'menu_class' => 'sister-pages',

s);

wp_nav_menu($menu_args);

 


 

How-to get a menu label via $post-> or $page->ID

(현재 페이지 1depth 메뉴의 label 추출)

링크:  http://stackoverflow.com/questions/22806170/how-to-get-a-menu-label-via-post-or-page-id

functions.php에 다음 코드 추가

function get_menu_label_by_post_id($post_id, $menu) {

$menu_title = '';
$nav = wp_get_nav_menu_items($menu);

foreach ( $nav as $item ) {

if ( $post_id == $item->object_id ) {
$menu_title = $item->post_title;

break;

}

}

return ($menu_title !== '') ? $menu_title : get_the_title($post_id);

}

 

sidebar.php에 다음 코드 추가

echo get_menu_label_by_post_id($post->ID, 'gnb');

Total 0

    Total 72
    NumberTitleAuthorDateVotesViews
    52
    Title tag에 사이트 제목과 태그라인이 중복 출력되는 문제
    북극海 | 2019.12.25 | Votes 0 | Views 2527
    북극海2019.12.2502527
    51
    DB이관 중 max_allowed_packet 오류 시 해결 방법
    북극海 | 2019.09.19 | Votes 0 | Views 2086
    북극海2019.09.1902086
    50
    유용한 SEO 도구
    북극海 | 2019.07.25 | Votes 0 | Views 2206
    북극海2019.07.2502206
    49
    SQL join 예제
    북극海 | 2019.06.07 | Votes 0 | Views 2378
    북극海2019.06.0702378
    48
    워드프레스 + 오라클DB 조합으로 웹사이트 개발이 가능할까요?
    북극海 | 2017.09.18 | Votes 0 | Views 3638
    북극海2017.09.1803638
    47
    워드프레스 안드로이드 앱 개발 관련
    북극海 | 2017.04.10 | Votes 0 | Views 2878
    북극海2017.04.1002878
    46
    "grep: Argument list too long" error?
    북극海 | 2017.04.05 | Votes 0 | Views 4006
    북극海2017.04.0504006
    45
    Linux 포트 프로세스 목록 확인
    북극海 | 2017.03.31 | Votes 0 | Views 2815
    북극海2017.03.3102815
    44
    멤버쉽 플러그인 참고
    북극海 | 2016.11.25 | Votes 0 | Views 2807
    북극海2016.11.2502807
    43
    Blog-style 해외 테마 리스트
    북극海 | 2016.10.10 | Votes 0 | Views 2632
    북극海2016.10.1002632
    42
    Showing the current page’s parent’s sub-menu items from a custom nav menu in WordPress
    북극海 | 2016.09.21 | Votes 0 | Views 3089
    북극海2016.09.2103089
    41
    responsive lightbox 정리
    북극海 | 2016.04.13 | Votes 0 | Views 1904
    북극海2016.04.1301904
    40
    BUILDING A NETFLIX FOR VR
    북극海 | 2016.04.13 | Votes 0 | Views 1910
    북극海2016.04.1301910
    39
    js libraries 정리
    북극海 | 2016.04.13 | Votes 0 | Views 1611
    북극海2016.04.1301611
    38
    React/REST API theme 튜토리얼
    북극海 | 2016.03.11 | Votes 0 | Views 1745
    북극海2016.03.1101745
    37
    그누프레스 매뉴얼 링크
    북극海 | 2016.03.02 | Votes 0 | Views 2284
    북극海2016.03.0202284
    36
    중국 쇼핑몰 제작 참고 자료
    북극海 | 2016.02.23 | Votes 0 | Views 1936
    북극海2016.02.2301936
    35
    워드프레스에서 페이스북, 네이버, 카카오 로그인 사용하기 참고 문서
    북극海 | 2016.02.23 | Votes 0 | Views 2143
    북극海2016.02.2302143
    34
    [WP-Members] redirect after login
    북극海 | 2016.02.18 | Votes 0 | Views 1965
    북극海2016.02.1801965
    33
    중국 도메인을 카페24 호스팅으로 연결할 때 팁!!!
    북극海 | 2016.02.16 | Votes 0 | Views 2225
    북극海2016.02.1602225

    BCT Ai Chatbot

    답변을 준비중입니다 . . .