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
3056

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
Number Title Author Date Votes Views
20
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:2.5.1:compile (default-compile) on project nomu:
BCTONE | 2023.02.16 | Votes 0 | Views 420
BCTONE 2023.02.16 0 420
19
Win] 포트 리스트 및 PID 삭제
북극海 | 2020.03.15 | Votes 0 | Views 2015
북극海 2020.03.15 0 2015
18
[전자정부 표준프레임워크]Several ports (8005, 8080, 8009) required by Tomcat v8.0 Server at localhost are already in use.
북극海 | 2020.03.15 | Votes 0 | Views 1586
북극海 2020.03.15 0 1586
17
SQL join 예제
북극海 | 2019.06.07 | Votes 0 | Views 2343
북극海 2019.06.07 0 2343
16
워드프레스 + 오라클DB 조합으로 웹사이트 개발이 가능할까요?
북극海 | 2017.09.18 | Votes 0 | Views 3607
북극海 2017.09.18 0 3607
15
워드프레스 안드로이드 앱 개발 관련
북극海 | 2017.04.10 | Votes 0 | Views 2847
북극海 2017.04.10 0 2847
14
"grep: Argument list too long" error?
북극海 | 2017.04.05 | Votes 0 | Views 3974
북극海 2017.04.05 0 3974
13
Linux 포트 프로세스 목록 확인
북극海 | 2017.03.31 | Votes 0 | Views 2780
북극海 2017.03.31 0 2780
12
Showing the current page’s parent’s sub-menu items from a custom nav menu in WordPress
북극海 | 2016.09.21 | Votes 0 | Views 3056
북극海 2016.09.21 0 3056
11
카카오톡 링크 버튼 추가 시 참고
북극海 | 2016.02.14 | Votes 0 | Views 1875
북극海 2016.02.14 0 1875
10
Secret SNS 기능 추가 관련 정리
북극海 | 2016.01.19 | Votes 0 | Views 1
북극海 2016.01.19 0 1
9
#1366 - Incorrect string value 오류 해결 방법
북극海 | 2015.06.23 | Votes | Views 1817
북극海 2015.06.23 1817
8
DB 접속이 가능한 상태에서 관리자 이메일/비밀번호 잃어버렸을 때 복구 방법
북극海 | 2015.06.17 | Votes | Views 2193
북극海 2015.06.17 2193
7
[기사] '액티브X' 없는 간편결제, 온라인몰의 대안은
북극해 | 2015.03.25 | Votes | Views 1705
북극해 2015.03.25 1705
6
페이지 또는 글 본문 이미지가 갑자기 보이지 않을 때 해결 방법
북극해 | 2015.03.14 | Votes | Views 2127
북극해 2015.03.14 2127
5
워드프레스 에디터에서 글 작성 중 특수문자 입력할 때 팁입니다
북극해 | 2015.02.06 | Votes | Views 1826
북극해 2015.02.06 1826
4
png -> ico
경제교육 | 2015.02.06 | Votes | Views 1572
경제교육 2015.02.06 1572
3
그누보드 관리자 비밀번호 분실 시 해결 방법 (SQL 실행)
북극해 | 2015.02.05 | Votes | Views 1741
북극해 2015.02.05 1741
2
ai -> png 변환
맥심커피 | 2015.01.29 | Votes | Views 2279
맥심커피 2015.01.29 2279
1
반응형 웹사이트인지 확인하는 서비스
toptop | 2015.01.25 | Votes | Views 2016
toptop 2015.01.25 2016

BCT Ai Chatbot

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