Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Taxonomy hierarchical navigation on reload WordPress

  • SOLVED

Hey Guys,

sry about my english. i´ll give my best ;)

i need a taxonomy menu for my client. taxonomy name is "ls_product_cats"

Example:

- Tax 1
-- Tax 1.1
-- Tax 1.2
--- Tax 1.2.1
--- Tax 1.2.2
--- Tax 1.2.3
---- Tax 1.2.3.1
---- Tax 1.2.3.2
---- Tax 1.2.3.3
--- Tax 1.2.4
-- Tax 1.3
- Tax 2
- Tax 3
- Tax 4
- Tax 5

I can use

<?php
$strTaxonomy = 'ls_products_cat';

$args = array(
'title_li' => '',
'taxonomy' => $strTaxonomy,
'orderby' => 'slug'
);
wp_list_categories( $args );
?>


but my client would like to have it by reload
click Tax 1 -> you see all Tax 1.x
click on Tax 1.x -> you see all Tax 1.x.x
click on Tax 1.x.x -> you see all Tax 1.x.x.x

Example:
http://www.shopwaredemo.de/genusswelten/tees-und-zubehoer/tees/

Markus

Answers (3)

2014-09-23

timDesain Nanang answers:

Put this code into theme's functions.php,
then put shortcode <strong>[product_menu]</strong> into text widget.

function wpq_taxonomy_top( $current, $taxonomy ) {
$output = '';
$start = get_term( $current, $taxonomy);
if(!empty( $start ) && !is_wp_error( $start ) ){
$start_top = $start->parent;

if($start_top>0){
$output .= $start_top.',';
$output .= wpq_taxonomy_top( $start_top, $taxonomy );
}
}
$output = trim($output, ',');
return $output;
}

add_shortcode('product_menu', 'wpq_product_menu');
function wpq_product_menu( $atts, $content = null ) {
global $wpdb, $post, $wp_query;

$taxonomy = 'ls_product_cats'; //change this tax as yours

$current = $wp_query->get_queried_object_id();
$tops = explode(',', wpq_taxonomy_top( $current, $taxonomy ));

$output = '';
$output .= '<ul class="products-ul">';
$term_args = array('parent' => 0, 'orderby'=>'name', 'order'=>'ASC', 'hide_empty'=>false, );
$get_terms = get_terms($taxonomy, $term_args);
foreach($get_terms as $parent) {
$parent_id = $parent->term_id;

$output .= '<li>';
$output .= '<a href="'.esc_url( get_term_link( $parent ) ) .'">'.$parent->name.'</a>';
$has_child = get_term_children($parent_id, $taxonomy);
if(in_array($parent_id, $tops) OR $parent_id==$current){
$output .= wpq_taxonomy_child($taxonomy, $parent_id, $current);
}
$output .= '</li>';
}
$output .= '</ul>';

return $output;
}

function wpq_taxonomy_child($taxonomy, $parent_id, $current, $recursive='yes'){
global $wpdb;
$tops = explode(',', wpq_taxonomy_top( $current, $taxonomy ));

$output = '';
if($parent_id>0){
$child_args = array('parent' => $parent_id, 'orderby'=>'name', 'order'=>'ASC', 'hide_empty'=>false, );
$get_childs = get_terms($taxonomy, $child_args);
if(!empty( $get_childs ) && !is_wp_error( $get_childs ) ){
$output .= '<ul>';
foreach($get_childs as $child) {
$child_id = $child->term_id;

$output .= '<li>';
$output .= '<a href="'.esc_url( get_term_link( $child ) ) .'" id="cat_'.$child_id.'">'.$child->name.'</a>';
if(in_array($child_id, $tops) OR $child_id==$current){
$output .= wpq_taxonomy_child($taxonomy, $child_id, $current);
}
$output .= '</li>';
}
$output .= '</ul>';
}
}

return $output;
}


b0li comments:

your code works :)

thanks mate

how can i assign the money to you?
if i open the tutorial page i get a 500 Server error :(

greets

2014-09-23

Arnav Joy answers:

try this


<?php
global $wp_query;
$term_id = $wp_query->get_queried_object_id();

if( $term_id > 0 )
$parent = $term_id ;
else
$parent = 0;

$strTaxonomy = 'ls_products_cat';



$args = array(

'title_li' => '',

'taxonomy' => $strTaxonomy,

'child_of' => $parent,

'orderby' => 'slug'

);

wp_list_categories( $args );

?>


b0li comments:

i have added an example above


Arnav Joy comments:

Please show me your site.

2014-09-23

Hariprasad Vijayan answers:

Hi,

I hope you could try this plugin https://wordpress.org/plugins/collapsing-categories/.

Regards,
Hariprasad