Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

share|improve this question
    
Found the solution by editing the walk() function pastebin.com/Kj6Q502h Its a bit of a hack because it assumes that you are only looking at a depth of 1. Any other solutions welcome. –  Brady Sep 7 '11 at 10:53

1 Answer 1

up vote 0 down vote accepted

This is what I went with in the end. Although it only will work correctly if you are using a depth of one:

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) {
        global $post;
        extract($args, EXTR_SKIP);
        $css_class = array();

        if("partner" == get_post_type( $post ))
            $current_page = get_ID_by_slug("about-us/our-partners");

        if ( !empty($current_page) ) {
            if ( $page->ID == $current_page )
                $css_class[] = 'selected';
        }

        $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, $a, $b ) {
        foreach($elements as $element) {
            if($a['child_of'] == $element->post_parent)
                $this->count++;
        }
        return parent::walk( $elements, $max_depth, $a, $b );
    }
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.