• Hi,
    I want to list the 2nd level terms of a taxonomy. I can control to show just parents but I don’t know how to do it just for an specific level…

Viewing 6 replies - 1 through 6 (of 6 total)
  • I’m trying do the same, but ..
    I found two ways.
    1. via get_terms, you have to always go for 1 level + two levels and then: second minus first, and you have the second.
    same thing works with the wp_list_categories

    the problem is that once you use the array_diff function then there is no way, or I haven’t found one how properly alphabetically sort it…in utf-8 with non-english alphabet.

    don’t ask me why, if I know, I have the solution.

    In other words it’s really complicate.. I still do not have the solution

    Thread Starter jepser

    (@jepser)

    Hi there I found the solution! ??
    And I post it on my blog:
    https://jepserbernardino.com/mini-tutoriales/crear-anidados-con-categoriastaxonomias-en-wordpress/

    Hope it helps!

    grate, thanks

    here is my solution. By this you can go as deep as you like to.
    Right this isn’t the answer for my question…this code is good when you want to build the breadcrumbs (the next example will list all the terms form each level)

    $the_venue = wp_get_object_terms( $post->ID, 'the_taxonomy', array('fields'=>'ids'));
    $args=array(
      'name' => 'the_taxonomy'
    );
    $output = 'name';
    $taxonomies=get_taxonomies($args,$output);
    if  ($taxonomies) {
      foreach ($taxonomies  as $taxonomy ) {
        $the_slug =  $taxonomy->rewrite[slug];
      }
    }
    $term = get_term_by( 'id', $the_venue[0], 'the_taxonomy');
    $term2 = get_term_by( 'id', $term->parent, 'the_taxonomy');
    $term3 = get_term_by( 'id', $term2->parent, 'the_taxonomy');
    
    if (!empty($term->term_id) || !empty($term2->term_id) || !empty($term3->term_id)){
    	echo '<div>';
    		if (empty($term3->errors) && !empty($term3)){
    			echo '<a href="';
    			bloginfo('url');
    			echo '/'.$the_slug.'/'.$term3->slug.'" >'.$term3->name.'</a>';
    		}
    		if (empty($term2->errors) && !empty($term2)){
    			echo '<a href="';
    			bloginfo('url');
    			echo '/'.$the_slug.'/'.$term2->slug.'" >'.$term2->name.'</a>';
    		}
    		if (empty($term->errors) && !empty($term)){
    			echo '<a href="';
    			bloginfo('url');
    			echo '/'.$the_slug.'/'.$term->slug.'" >'.$term->name.'</a>';
    		}
    	echo '</div>';
    }

    It gets all the highest $terms and then it looks for their parents etc.

    here is the actual solution:
    It prints out all the terms from chosen level. As you can see it’s tiresome and looong and dirty.. I hope they make some function for us. ;/

    $top_level_arg = array(
    		'style'    => 'none',
    		'orderby'   => 'name',
    		'order'     => 'ASC',
    		'hide_empty' => 0,
    		'title_li' => '',
    		'depth' => '1',
    		'taxonomy'  => 'the_taxonomy',
    		'echo'      => 0
    	);
    	$sec_level_arg = array(
    		'style'    => 'none',
    		'hierarchical' => 1,
    		'orderby'   => 'name',
    		'order'     => 'ASC',
    		'hide_empty' => 0,
    		'title_li' => '',
    		'depth' => '2',
    		//'child_of'  => $decka,
    		'taxonomy'  => 'the_taxonomy',
    		'echo'      => 0
    	);
    	$third_level_arg = array(
    		'style'    => 'none',
    		'orderby'   => 'name',
    		'order'     => 'ASC',
    		'hide_empty' => 0,
    		'title_li' => '',
    		'depth' => '3',
    		'taxonomy'  => 'the_taxonomy',
    		'echo'      => 0
    	);
    
    	// make arrays
    	$top_level_arg_exploaded = explode('<br />',wp_list_categories($top_level_arg));
    	$sec_level_arg_exploaded = explode('<br />',wp_list_categories($sec_level_arg));
    	$third_level_arg_exploaded = explode('<br />',wp_list_categories($third_level_arg));
    
    	// substract levels
    	$places = array_diff($third_level_arg_exploaded,$sec_level_arg_exploaded );
    	asort($places);
    	$cities = array_diff($sec_level_arg_exploaded, $top_level_arg_exploaded);
    	asort($cities);
    	$states = $top_level_arg_exploaded; // necistene, ale orizle
    
    	// choose what level you want to see
    	$level = 1;
    	// this my own check, it shows certain hyerarchies only on given pages -
    	if ($level == 1){
    		$test = implode('',$places);
    	}elseif ($level == 3){
    		$test = implode('',$cities);
    	}elseif (level == 3){
    		$test = implode('',$states);
    	}
    
    	// build the collumns with links
    	$regexp = "<a\s[^>]*href=(\"??)([^\" >]*?)\\1[^>]*>(.*)<\/a>";
    	if(preg_match_all("/$regexp/siU", $test, $matches, PREG_SET_ORDER)) {
    		 foreach($matches as $match) {
    			 // create new arra name->link for later sorting
    			 $c[] = array('name' =>  $match[3], 'link' => $match[2]);
    		 }
    	}
    
    	foreach ($c as $tag){
    		echo $tag .'<br />';
    	}

    I found out a bug if it is actually bug.
    When I call the second and higher level via wp_list_categories it doesn’t sort the items correctly. Don’t ask me why.

    I’ll let you know once I know the solution.

    you know why?
    because I use the xlanguage(plugin) along, so the name has 2 possibilities, based on the language you are browsing.
    But the sorting solution is based on the original name.
    then “Curich|Zurich” will always pop up on “C” position, although you see the “Zurich” lang. version

    so don’t be confused the script above works ??

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘How to list 2nd level of a taxonomy terms’ is closed to new replies.