我需要帮助分离子页面,这是我现在拥有的:
<ul class="children">
<li class="page_item page-item-54 page_item_has_children"><span>Title of Child1</span>
<ul class="children">
<li class="page_item page-item-60"><a>Grandchild1</a></li>
<li class="page_item page-item-62"><a>Grandchild2</a></li>
</ul>
</li>
<li class="page_item page-item-58 page_item_has_children"><span>Title of Child2</span>
<ul class="children">
<li class="page_item page-item-64"><a>Grandchild1</a></li>
<li class="page_item page-item-66"><a>Grandchild2</a></li>
</ul>
</li>
</ul>
这就是我想要实现的目标:
<div class="children-list">
<ul class="children">
<span>Title of Child1</span>
<li class="page_item page-item-60"><a>Grandchild1</a></li>
<li class="page_item page-item-62"><a>Grandchild2</a></li>
</ul>
<ul class="children">
<span>Title of Child2</span>
<li class="page_item page-item-64"><a>Grandchild1</a></li>
<li class="page_item page-item-66"><a>Grandchild2</a></li>
</ul>
</div>
这是我用于wp\\u list\\u页面的助行器
class series_walker extends Walker_Page {
public function start_el( &$output, $page, $depth = 0, $args = array(), $current_page = 0 ) {
if ( isset( $args[\'item_spacing\'] ) && \'preserve\' === $args[\'item_spacing\'] ) {
$t = "\\t";
$n = "\\n";
} else {
$t = \'\';
$n = \'\';
}
if ( $depth ) {
$indent = str_repeat( $t, $depth );
} else {
$indent = \'\';
}
$css_class = array( \'page_item\', \'page-item-\' . $page->ID );
if ( isset( $args[\'pages_with_children\'][ $page->ID ] ) ) {
$css_class[] = \'page_item_has_children\';
}
if ( ! empty( $current_page ) ) {
$_current_page = get_post( $current_page );
if ( $_current_page && 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_classes = implode( \' \', apply_filters( \'page_css_class\', $css_class, $page, $depth, $args, $current_page ) );
if ( \'\' === $page->post_title ) {
/* translators: %d: ID of a post */
$page->post_title = sprintf( __( \'#%d (no title)\' ), $page->ID );
}
$args[\'link_before\'] = empty( $args[\'link_before\'] ) ? \'\' : $args[\'link_before\'];
$args[\'link_after\'] = empty( $args[\'link_after\'] ) ? \'\' : $args[\'link_after\'];
$atts = array();
$atts[\'href\'] = get_permalink( $page->ID );
$output .= $indent . sprintf(
\'<li class="%s">\'.(($depth == 1) ? \'<span%s>%s%s%s</span>\' : \'<a%s>%s%s%s</a>\'),
$css_classes,
$attributes,
$args[\'link_before\'],
/** This filter is documented in wp-includes/post-template.php */
apply_filters( \'the_title\', $page->post_title, $page->ID ),
$args[\'link_after\']
);
}
}
非常感谢您的帮助,我已经尝试了两天了。非常感谢。
SO网友:signal2013
我不确定仅此类就足以修改所需的输出。您可能需要在开始时扩展其他部分。(我还没有对此进行验证。)
同时,这里有一个想法。。。(这是一个有趣的方法)菜单按当前方式生成后,执行regex查找和替换以按照您想要的方式重建列表。
注意:如果正则表达式中出现任何错误,此方法可能会产生意外的结果。因此,它不应该用于对应用程序至关重要的任何事情。
说出wp_list_pages
值保存到变量$mylist,然后重试。
$mylist = preg_replace(\'/^\\s*\\<ul[^>]*\\>/is\', \'<div class="children-list">\', $mylist);
$mylist = preg_replace(\'/\\<\\/ul\\>\\s*$/is\', \'</div>\', $mylist);
$mylist = preg_replace(\'/\\<\\/ul\\>\\s*\\<\\/li>/is\', \'</ul>\', $mylist);
$mylist = preg_replace(\'/<li\\b[^>]*has\\_children[^>]*\\>(.*?)(<ul\\b[^>]*\\>)/is\', \'$2$1\', $mylist);
echo $mylist;
有了这个概念,您甚至不必使用walker扩展。您可以重做regex以适应原始输出。但我不知道这是什么,我使用了您在示例中提供的输出。