Custom HTML structure in wp_list_categories

In case someone finds this someday I was able to solve this and I used 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>";
    }

}

}

Leave a Comment