唷!比我想象的要复杂一点-只要过滤器page_css_class
传回$args
这将节省大量工作(可能会将此提交给trac)罢工>
Since 3.3 就这么简单!
function add_parent_class( $css_class, $page, $depth, $args )
{
if ( ! empty( $args[\'has_children\'] ) )
$css_class[] = \'parent\';
return $css_class;
}
add_filter( \'page_css_class\', \'add_parent_class\', 10, 4 );
对于那些使用WordPress 3.3之前版本的用户,这里有一个关键点:
/**
* Extend the default page walker class to append class names for pages that
* are parents.
* @uses Walker_Page
*/
class My_Page_Walker extends Walker_Page
{
/**
* Filter in the classes for parents.
*/
function _filterClass( $class )
{
$class[] = \'parent\'; // change this to whatever classe(s) you require
return $class;
}
/**
* This is effectively a wrapper for the default method, dynamically adding
* and removing the class filter when the current item has children.
*/
function start_el( &$output, $page, $depth, $args, $current_page )
{
if ( !empty($args[\'has_children\']) )
add_filter( \'page_css_class\', array( &$this, \'_filterClass\') );
parent::start_el( $output, $page, $depth, $args, $current_page );
if ( !empty($args[\'has_children\']) )
remove_filter( \'page_css_class\', array( &$this, \'_filterClass\') );
}
}
我建议把这门课安排在
functions.php
, 然后,您可以在任何地方使用它。
要使用它,请致电wp_list_pages()
像这样;
// You need to pass the walker argument to wp_list_pages(). You must use an
// array to do this.
wp_list_pages(array(
\'walker\' => new My_Page_Walker,
\'title_li\' => \'\'
));