WordPress: visualizzare la gerarchia delle tassonomie dei custom post type

WordPress: visualizzare la gerarchia delle tassonomie dei custom post type

Se usate i custom post type con tassonomie custom può essere utile visualizzare le tassonomie associate ad un dato post come se stessimo visualizzando dei tag e delle categorie. Ecco come fare.

La seguente funzione accetta come parametro l'ID del custom post type.


function my_get_attributes( $id ) {
	$type = get_post_type( $id );
	$taxonomies = get_object_taxonomies( $type, 'objects' );
	$output = array();
	$html = '';
	$i = -1;
  	foreach ( $taxonomies as $taxonomy_slug => $taxonomy ) {
  		$i++;
  		$terms = get_the_terms( $id, $taxonomy_slug );
  		$output[$i] = array();
  		$output[$i]['title'] = $taxonomy->label;
  		 if ( !empty( $terms ) ) {
  		 	$output[$i]['labels'] = array();
  		 	foreach ( $terms as $term ) {
  		 		$output[$i]['labels'][] = $term->name;
  		 	}
  		 }
  	}
  	$j = 1;
  	foreach( $output as $value ) {
  		$j++;
  		$title = $value['title'];
  		$labels = $value['labels'];
  		
  		$html .= $title . '<br/>';
  		
  		foreach( $labels as $label ) {
  			$html .= sprintf( '<span>%s</span>', $label );
  		}
  		
  	}
  	
  	
	
	echo $html;
}

Quindi quando create un Loop di custom post type potete usare questa funzione per visualizzare la gerarchia delle tassonomie. Ovviamente la marcatura che ho proposto è puramente indicativa.

Torna su