WordPress : Structure HTML personnalisée dans wp_list_categories
J’essaie d’accomplir une navigation Ajax pour les catégories et pour cela j’ai besoin que le code ressemble à:
<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>
J’ai déjà défini un marcheur qui crée quelque chose de similaire, en ajoutant également une div autour des enfants ul et une classe aux catégories qui ne contiennent aucune catégorie d’enfants. Cela fonctionne bien pour le premier niveau de sous-catégories, mais je suis resté bloqué là-bas et je n’arrive pas à créer correctement le niveau suivant de sous-catégories. Voici le marcheur que j’ai :
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'>$linkn$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'>$linkn";
}
if( $depth > 1) {
$link = '<a href="#"' . '>' . $cat_name . '</a>';
$output .= "n<li class='sub-category $class' data-level='$n_depth'>$linkn";
}
}
// 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>";
}
}
}
Quelqu’un a des idées ?
Solution n°1 trouvée
Au cas où quelqu’un trouverait cela un jour, j’ai pu résoudre ce problème et j’ai utilisé ce marcheur:
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'>$linkn$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'>$linkn";
}
// if( $depth > 1) {
// $link = '<a href="#">' . $cat_name . '</a>';
// $output .= "n<li class='sub-category $class' data-level='$n_depth'>$linkn";
// }
}
// 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>";
}
}
}
0 commentaire