Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function custom_taxonomies_terms_links()
- {
- // get post by post id
- $post = get_post( $post->ID );
- // get post type by post
- $post_type = $post->post_type;
- // get post type taxonomies
- $taxonomies = get_object_taxonomies( $post_type, 'objects' );
- $out = '';
- $format_start_list = "<h2>%s</h2><ul>\n";
- $format_start_parent = "<li><h3>%s</h3><ul>\n";
- $format_item = "<li><a href=\"%s\">%s</a></li>\n";
- $format_end_parent = "</ul></li>\n";
- $format_end_list = "</ul>\n";
- foreach ( $taxonomies as $taxonomy_slug => $taxonomy ) {
- $ordered_terms = array();
- // get the terms related to post
- $terms = get_the_terms( $post->ID, $taxonomy_slug );
- if ( !empty( $terms ) ) {
- $out .= sprintf($format_start_list, $taxonomy->label);
- foreach ( $terms as $term ) {
- if ( $term->parent < 1 ) {
- $ordered_terms[$term->term_id]['parent'] = $term;
- } else {
- $ordered_terms[$term->parent]['items'][$term->term_id] = $term;
- }
- }
- foreach ($ordered_terms as $id => $terms) {
- $out .= sprintf($format_start_parent, $terms['parent']->name);
- foreach ($terms['items'] as $term) {
- $out .= sprintf($format_item, get_term_link($term->slug, $taxonomy_slug), $term->name);
- }
- $out .= $format_end_parent;
- }
- $out .= $format_end_list;
- }
- }
- return $out;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement