This question follows on from this one:
Bar separated navigation by extending Walker_Nav_Menu
I’m trying to do the same thing but this time I’m extending Walker_Page
instead.
The issue I’m having is that I’m setting the depth to 1 and in that depth there might be 4 pages. But in the walk()
the number of elements returned are more. Because the number of elements returned are more than the amount of pages being showed then I get one bar |
too many added to the end of my menu.
Here is the code I have so far:
<?php
$children = get_pages('child_of='.$post->ID);
if( 0 != count( $children ) ) {
$child_of = $post->ID;
} else {
$child_of = $post->post_parent;
}
$args = array(
'depth' => 1,
'child_of' => $child_of,
'title_li' => '',
'echo' => 1,
'sort_column' => 'menu_order, post_title',
'walker' => new Bar_List_Walker_Page
);
wp_list_pages( $args );
?>
My Walker:
class Bar_List_Walker_Page extends Walker_Page {
public $count;
public $running_count;
function __construct() {
$this->count = 0;
$this->running_count = 0;
}
function start_el(&$output, $page, $depth, $args, $current_page) {
extract($args, EXTR_SKIP);
$css_class = array();
if ( !empty($current_page) ) {
$_current_page = get_page( $current_page );
_get_post_ancestors($_current_page);
if ( isset($_current_page->ancestors) && in_array($page->ID, (array) $_current_page->ancestors) )
$css_class[] = 'current_page_ancestor';
if ( $page->ID == $current_page )
$css_class[] = 'current_page_item';
elseif ( $_current_page && $page->ID == $_current_page->post_parent )
$css_class[] = 'current_page_parent';
} elseif ( $page->ID == get_option('page_for_posts') ) {
$css_class[] = 'current_page_parent';
}
$css_class = implode(' ', apply_filters('page_css_class', $css_class, $page));
$output .= '<a class="' . $css_class . '" href="' . get_permalink($page->ID) . '">' . apply_filters( 'the_title', $page->post_title, $page->ID ) . '</a>';
}
function end_el(&$output, $item, $depth) {
$this->running_count++;
if($this->count > $this->running_count)
$output .= " | ";
}
function walk( $elements, $max_depth, $r ) {
$this->count = count($elements);
return parent::walk( $elements, $max_depth, $r );
}
}
How do I get my walker to get the correct amount of pages in the level I’m targeting?
This is what I went with in the end. Although it only will work correctly if you are using a depth of one: