扩展Walker_Page实现条块分隔导航

时间:2011-09-07 作者:Scott

这个问题是从这个问题开始的:

Bar separated navigation by extending Walker_Nav_Menu

我也在试着做同样的事情,但这次我在扩展Walker_Page 相反

我遇到的问题是,我将深度设置为1,在该深度中可能有4页。但是在walk() 返回的元素数量更多。因为返回的元素数量超过了显示的页面数量,所以我得到了一个条| 菜单末尾添加了太多内容。

以下是我目前掌握的代码:

<?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 );
?>
我的步行者:

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 );
    }
}
如何让我的助行器在我的目标级别中获得正确的页数?

1 个回复
最合适的回答,由SO网友:Scott 整理而成

这就是我最后要做的。尽管只有在使用深度为1时,它才能正常工作:

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 );
    }
}

结束