1

I've created a custom taxonomy (locations) which is hierarchical, and would like to: a) display an unordered list that respects the hierarchy AND b)customize the display output so that I can specify a custom url

Here's the code I'm using, including the two different ways I'm outputting.

<?php 
$args = array(
'taxonomy'     => 'locations',
'orderby'      => 'name',
'hide_empty'   => 0,
'title_li'     => '',
'hierarchical' => 1,
'walker'       => null,
);
?>    

// The following gives me the hierarchical display

<ul class="menu">
<?php wp_list_categories( $args ); ?>
</ul>

// And this gives me the customized links

<?php 
$categories = get_categories($args);
foreach($categories as $category) { 
echo '<li><a href="http://website.com/?ls=&location=' . $category->slug . '">' . $category->name.'</a></li>';
} 

?>

The problem is making BOTH of those things happen.

Any solutions? I'm guessing it might be a custom Walker, but I'm not savvy enough to figure that out without a little guidance!!

2 Answers 2

1

Solved it with the following Custom Walker:

class CustomWalker extends Walker_Category {
function start_el(&$output, $item, $depth=0, $args=array()) {  
$output .= "\n<li><a href=\"http://website.com/?ls=&location=" . $item->slug . "\">".esc_attr($item->name);
}  

function end_el(&$output, $item, $depth=0, $args=array()) {  
$output .= "</li>\n";  
}  
}  
1
  • You should edit your answer with the full pertinent code (include $args and the wp_list_categories() call), then accept it, so that other users could with the same problem could find it in the future. Commented Sep 8, 2017 at 22:19
0

I may be mis-understanding but you don't need wp_list_categories for this. (also your coding is missing some details).

Will this work?:

<?php 
$args = array(
'taxonomy'     => 'locations',
'orderby'      => 'name',
'hide_empty'   => 0,
);
?>    

// And this gives me the customized links

<?php 
$categories = get_categories($args);
echo '<ul class="menu">';
foreach($categories as $category) { 
    echo '<li><a href="http://website.com/?ls=&location=' . $category->slug . '">' . $category->name.'</a></li>';
} 
echo '</ul>';
?>
1
  • Thank you for your response! I tried that, but it didn't nest the child categories under their parents - it just output a flat list.
    – jwyson
    Commented Sep 8, 2017 at 22:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.