我正在尝试实现类别的Ajax导航,为此,我需要代码如下所示:
<ul>
<li data-level="1">Parent Category</li>
<li data-level="1">Parent Category
<ul class="children">
<li class="sub-category" data-level="2">Subcategory
<ul class="children">
<li class="sub-sub-category" data-level="3">Sub Subcategory</li>
</ul>
</li>
</ul>
</li>
</ul>
我已经定义了一个walker,它创建了一些类似的东西,还在子类ul周围添加了一个div,并在不包含任何子类的类别中添加了一个类。它在第一级子类别中运行良好,但我被困在那里,似乎无法正确创建下一级子类别。这是我的助行器:
class Navigation_Catwalker extends Walker_Category {
// Configure the start of each level
function start_lvl(&$output, $depth = 0, $args = array()) {
$output .= "";
}
// Configure the end of each level
function end_lvl(&$output, $depth = 0, $args = array()) {
$output .= "";
}
// Configure the start of each element
function start_el( &$output, $category, $depth = 0, $args = array(), $id = 0) {
// Set the category name and slug as a variables for later use
$cat_name = esc_attr( $category->name );
$cat_name = apply_filters( \'list_cats\', $cat_name, $category );
$cat_slug = esc_attr( $category->slug );
$n_depth = $depth + 1;
$termchildren = get_term_children( $category->term_id, $category->taxonomy );
if(count($termchildren)===0){
$class .= \'i-dont-have-kids\';
}
// Configure the output for the top list element and its URL
if ( $depth === 0 ) {
$link = \'<a class="parent-category-dropdown" href="#"\' . \'>\' . $cat_name . \'</a>\';
$indent = str_repeat("\\t", $depth);
$output .= "\\t<li class=\'parent-category $class " . $cat_slug . "\' data-level=\'$n_depth\'>$link\\n$indent<div class=\'children\'><ul>\\n<li class=\'parent-name\' data-level=\'$n_depth\'>" . $cat_name . "</li>";
}
// Configure the output for lower level list elements and their URL\'s
if ( $depth === 1 ) {
$link = \'<a href="#"\' . \'>\' . $cat_name . \'</a>\';
$output .= "\\t<li class=\'sub-category $class\' data-level=\'$n_depth\'>$link\\n";
}
if( $depth > 1) {
$link = \'<a href="#"\' . \'>\' . $cat_name . \'</a>\';
$output .= "\\n<li class=\'sub-category $class\' data-level=\'$n_depth\'>$link\\n";
}
}
// Configure the end of each element
function end_el(&$output, $page, $depth = 0, $args = array() ) {
if ( $depth === 0 ) {
$indent = str_repeat("\\t", $depth);
$output .= "$indent</ul>\\n</div>\\n";
}
if ( $depth > 0 ) {
$output .= "</li>";
}
}
}
有人有什么想法吗?
SO网友:Sara Vieira
如果有一天有人发现了这个问题,我能够解决这个问题,我使用了tis walker:
class Navigation_Catwalker extends Walker_Category {
// Configure the start of each level
function start_lvl(&$output, $depth = 0, $args = array()) {
$output .= "";
}
// Configure the end of each level
function end_lvl(&$output, $depth = 0, $args = array()) {
$output .= "";
}
// Configure the start of each element
function start_el( &$output, $category, $depth = 0, $args = array(), $id = 0) {
// Set the category name and slug as a variables for later use
$cat_name = esc_attr( $category->name );
$cat_name = apply_filters( \'list_cats\', $cat_name, $category );
$cat_slug = esc_attr( $category->slug );
$n_depth = $depth + 1;
$termchildren = get_term_children( $category->term_id, $category->taxonomy );
$class = \'\';
if(count($termchildren)===0){
$class .= \'i-dont-have-kids\';
}
// Configure the output for the top list element and its URL
if ( count($termchildren)>0) {
$link = \'<a class="parent-category-dropdown" href="#"\' . \'>\' . $cat_name . \'</a>\';
$indent = str_repeat("\\t", $depth);
$output .= "\\t<li class=\'parent-category $class " . $cat_slug . "\' data-level=\'$n_depth\'>$link\\n$indent<div class=\'children\'><ul>\\n<li class=\'parent-name\'>" . $cat_name . "</li>";
}
// Configure the output for lower level list elements and their URL\'s
if ( count($termchildren)===0) {
$link = \'<a href="#">\' . $cat_name . \'</a>\';
$output .= "\\t<li class=\'sub-category $class\' data-level=\'$n_depth\'>$link\\n";
}
// if( $depth > 1) {
// $link = \'<a href="#">\' . $cat_name . \'</a>\';
// $output .= "\\n<li class=\'sub-category $class\' data-level=\'$n_depth\'>$link\\n";
// }
}
// Configure the end of each element
function end_el(&$output, $category, $depth = 0,$args = array()) {
$termchildren = get_term_children( $category->term_id, $category->taxonomy );
if (count($termchildren)>0) {
$indent = str_repeat("\\t", $depth);
$output .= "$indent</ul>\\n</div>\\n";
}
if (count($termchildren)===0 ) {
$output .= "</li>";
}
}
}