Extended Walker_Page does not display custom start_lvl () or end_lvl ()

I have a custom class Walker_Page that I have expanded as follows:

 class List_Nav extends Walker_Page { function start_lvl( &$output, $depth = 0, $args = array() ) { $indent = str_repeat("\t", $depth); $output .= "\n$indent<ul class='ListNav'>\n"; } function start_el(&$output, $page, $depth = 0, $args = array(), $current_page = 0) { $output .= '<li class="ListNav-item">'; $output .= '<a class="ListNav-link" href="' . get_permalink($page->ID) . '">' . apply_filters( 'the_title', $page->post_title, $page->ID ) . '</a>'; $output .= '</li>'; } function end_lvl( &$output, $depth = 0, $args = array() ) { $indent = str_repeat("\t", $depth); $output .= "\n$indent</ul>\n"; } } 

But I do not get any results from the start_lvl or end_lvl . Is there something that I have lost here or that I need to return? Im getting the output <li> from start_el() .


Update Using

This is how I use the walker:

 if ($post->post_parent) { $ancestors=get_post_ancestors($post->ID); $root = count($ancestors) - 1; $top_parent = $ancestors[$root]; } else { $top_parent = $post->ID; } $page_list_args = array( 'child_of' => $top_parent, 'depth' => 0, 'title_li' => false, 'walker' => new List_Nav ); wp_list_pages($page_list_args); 
+5
source share
1 answer

It looks like start_lvl() and end_lvl() always called in a loop and never at the first level. This applies to all WordPress Walkers , such as Walker_Nav_Menu , Walker_Page and Walker_Category .

This is not very clear, but you can guess it when you look at the Core Walker code or when you read the Walker documentation about start_lvl() .

But in the documentation for Walker :: start_lvl, he just says that he ...

Launches a list before adding items.

So, perhaps what needs to be done is updating the docs.

+2
source

Source: https://habr.com/ru/post/1239352/


All Articles