Showing the current page’s parent’s sub-menu items from a custom nav menu in WordPress
Showing the current page’s parent’s sub-menu items from a custom nav menu in WordPress
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');
Number | Title | Author | Date | Votes | Views |
72 |
카페24 JSP 호스팅 war 파일 배포 방법
BCTONE
|
2023.07.17
|
Votes 0
|
Views 208
|
BCTONE | 2023.07.17 | 0 | 208 |
71 |
Ubuntu 방화벽 명령어 모음
BCTONE
|
2023.07.03
|
Votes 0
|
Views 207
|
BCTONE | 2023.07.03 | 0 | 207 |
70 |
좀비 프로세스 삭제 명령
BCTONE
|
2023.07.02
|
Votes 0
|
Views 220
|
BCTONE | 2023.07.02 | 0 | 220 |
69 |
Uncaught TypeError: wp.media is not a function
BCTONE
|
2023.04.15
|
Votes 0
|
Views 266
|
BCTONE | 2023.04.15 | 0 | 266 |
68 |
Anaconda prompt def 사용 시 IndentationError: expected an indented block 에러 해결
BCTONE
|
2023.03.23
|
Votes 0
|
Views 284
|
BCTONE | 2023.03.23 | 0 | 284 |
67 |
[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 428
|
BCTONE | 2023.02.16 | 0 | 428 |
66 |
table prefix 변경 후 조치 방법
북극海
|
2022.10.06
|
Votes 0
|
Views 357
|
북극海 | 2022.10.06 | 0 | 357 |
65 |
워드프레스 작업시 참고할 query 모음
북극海
|
2022.09.21
|
Votes 0
|
Views 379
|
북극海 | 2022.09.21 | 0 | 379 |
64 |
DB내 악성 코드 문제 다수 발생
북극海
|
2022.08.25
|
Votes 0
|
Views 504
|
북극海 | 2022.08.25 | 0 | 504 |
63 |
SecureSign 인증 발급되지 않을 때 해결 방법
북극海
|
2022.07.06
|
Votes 0
|
Views 430
|
북극海 | 2022.07.06 | 0 | 430 |
62 |
도메인 / 호스팅 / 메일서버 분리 설정 시 메일 세팅 방법
북극海
|
2022.02.24
|
Votes 0
|
Views 741
|
북극海 | 2022.02.24 | 0 | 741 |
61 |
카카오톡 인앱브라우저에서 이미지 크게 키우는 팁
북극海
|
2021.09.13
|
Votes 0
|
Views 532
|
북극海 | 2021.09.13 | 0 | 532 |
60 |
Wix에서 구입한 도메인을 외부 호스팅으로 연결하는 방법 정리
북극海
|
2020.10.31
|
Votes 0
|
Views 2496
|
북극海 | 2020.10.31 | 0 | 2496 |
59 |
GET 방식 앵커 HTML 특정 위치 이동
북극海
|
2020.04.14
|
Votes 0
|
Views 3317
|
북극海 | 2020.04.14 | 0 | 3317 |
58 |
http://에서 https://www
북극海
|
2020.03.21
|
Votes 0
|
Views 2548
|
북극海 | 2020.03.21 | 0 | 2548 |
57 |
PHP Errors 메시지 감추는 방법
북극海
|
2020.03.20
|
Votes 0
|
Views 2853
|
북극海 | 2020.03.20 | 0 | 2853 |
56 |
[Avada] Fusion Cache 삭제 방법
북극海
|
2020.03.19
|
Votes 0
|
Views 2855
|
북극海 | 2020.03.19 | 0 | 2855 |
55 |
Win] 포트 리스트 및 PID 삭제
북극海
|
2020.03.15
|
Votes 0
|
Views 2022
|
북극海 | 2020.03.15 | 0 | 2022 |
54 |
[전자정부 표준프레임워크]Several ports (8005, 8080, 8009) required by Tomcat v8.0 Server at localhost are already in use.
북극海
|
2020.03.15
|
Votes 0
|
Views 1596
|
북극海 | 2020.03.15 | 0 | 1596 |
53 |
[MySQL] 특정 문자열 교체 쿼리문
북극海
|
2020.02.27
|
Votes 0
|
Views 1431
|
북극海 | 2020.02.27 | 0 | 1431 |