Иерархический список с классам Walker_Page
-
Здравствуйте форумчане. Передо мной стоит задача вывести список дочерних страниц текущего родителя.
Пример:
у меня есть иерархический список страниц- 1.0
- 1.2.0
- 1.3.0
- 1.3.1
- 1.3.2
- 1.2.1
- 1.2.2
- 2.0
- 2.2.0
Так вот когда я нахожусь на странице 1.0 список выводил дочерние страницы 1.2.0 , 1.2.1 ,1.2.2, где дочерние страницы 1.3.0 , 1.3.1 , 1.3.2 родителя 1.2.0 должны быть скрыты так и для страницы 2.0 (вид списка ниже)
- 1.0
- 1.2.0
- 1.2.1
- 1.2.2
- 2.0
На зарубежном блоге нашел интересное решение классом Walker_Page
class DynamicPageMenu extends Walker_Page { function start_el(&$output, $page, $depth, $args, $current_page ) { if ( $depth ) { $indent = str_repeat("\t", $depth); } else { $indent = ''; } extract($args, EXTR_SKIP); $css_class = array('page_item', 'page-item-'.$page->ID); $_current_page = null; $_tmp_page = null; if (!empty($current_page)) { $_current_page = get_page( $current_page ); $_tmp_page = get_page( $_current_page->post_parent ); } if ($page->post_parent == 0) { // this is a top level page, so it must be printed } elseif (isset($_current_page->ancestors) && in_array($page->ID, (array) $_current_page->ancestors)) { // this is in the path for our current page so it must be printed $css_class[] = 'current_page_ancestor'; } elseif ($page->ID == $current_page) { // this is the current page to it must be printed $css_class[] = 'current_page_item'; } elseif (!is_null($_current_page) && $page->ID == $_current_page->post_parent) { // this page is the current page parent $css_class[] = 'current_page_parent'; } elseif (!is_null($_current_page) && $page->post_parent == $_current_page->ID) { // this page sits just below the current page so it must also be printed } elseif (!is_null($_tmp_page) && $_tmp_page->post_parent == $page->post_parent) { // this is a sibling of the current page parent so it must also be printed } elseif (!is_null($_current_page) && $_current_page->post_parent == $page->post_parent) { // this is a sibling of the current page so it must be printed } else { // reject everything else. return true; } $css_class = implode(' ', apply_filters('page_css_class', $css_class, $page)); $output .= $indent . '<li class="' . $css_class . '"><a href="' . get_permalink($page->ID) . '">' . $link_before . apply_filters( 'the_title', $page->post_title, $page->ID ) . $link_after . '</a>'; if ( !empty($show_date) ) { if ( 'modified' == $show_date ) $time = $page->post_modified; else $time = $page->post_date; $output .= " " . mysql2date($date_format, $time); } } function end_el( &$output, $page, $depth = 0, $args = array() ) { $output .= "</li>\n"; } }
где вставляем wp_lis_page
<?php wp_list_pages(array('walker' => new DynamicPageMenu(),'title_li'=>'')); ?>
Так вот данный класс работает отменно, но есть одно но.
Дочерние страницы не текущего родителя не отображаются (то что мы хотели), но отображается пустой
<ul class="children"> </ul>
Не пойму, как пройти проверку в функции function start_lvl чтобы не выводить
<ul class='children'>
function start_lvl( &$output, $depth = 0, $args = array() ) { $indent = str_repeat("\t", $depth); $output .= "\n$indent<ul class='children'>\n"; }
- Тема «Иерархический список с классам Walker_Page» закрыта для новых ответов.