可以通过创建自定义Walker类并在调用wp_list_pages()
作用
Walker课程的目的是
它只跟踪树的每个分支:它必须由其他类扩展,这些类告诉它对遇到的每个元素做什么。
将此类放入functions.php
文件或plugins
文件
class linkModifyWalker extends Walker_Page {
function start_lvl( &$output, $depth = 0, $args = array() ) {
$indent = str_repeat("\\t", $depth);
$output .= "\\n$indent<ul class=\'dropdown-menu children\'>\\n";
}
function start_el( &$output, $page, $depth = 0, $args = array(), $current_page = 0 ) {
if ( $depth )
$indent = str_repeat("\\t", $depth);
else
$indent = \'\';
extract($args, EXTR_SKIP);
$css_class = array(\'page_item\', \'page-item-\'.$page->ID);
if( isset( $args[\'pages_with_children\'][ $page->ID ] ) )
$css_class[] = \'page_item_has_children dropdown\';
if ( !empty($current_page) ) {
$_current_page = get_post( $current_page );
if ( in_array( $page->ID, $_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, $depth, $args, $current_page ) );
$output .= $indent . \'<li class="\' . $css_class . \'"><a href="#\' .$page->ID . \'">\' . $link_before . apply_filters( \'the_title\', $page->post_title, $page->ID ) . $link_after . \'</a>\';
}
}
我们刚刚创建
linkModifyWalker
类,我们需要将其对象传递给
wp_list_pages()
作为论据。
\'walker\' => new linkModifyWalker()
<ul id="markdown-toc">
<li><h3><a href="#contents">Contents</a></h3></li>
<ul id="my_cutom_type-list">
<?php
wp_list_pages( array(
\'post_type\'=> \'manual\',
\'walker\' => new linkModifyWalker()
) );
?>
</ul>
</ul>
了解更多关于Walker课程的信息。我建议您访问以下链接。
Understanding the Walker Class