Display current post custom taxonomy in WordPress
Asked Answered
C

2

8

I've created custom taxonomies on WordPress and I want to display the current post taxonomies on the post in a list.

I'm using the following code to display a custom taxonomy named "Job Discipline":

<ul>
            <?php $args = array('taxonomy' => 'job_discipline'); ?>
            <?php $tax_menu_items = get_categories( $args );
            foreach ( $tax_menu_items as $tax_menu_item ):?>
            <li>
                Job Discipline: <a href="<?php echo get_term_link($tax_menu_item,$tax_menu_item->taxonomy); ?>">
                    <?php echo $tax_menu_item->name; ?>
                </a>
            </li>
            <?php endforeach; ?>
</ul>

That's just one of many taxonomies I want to list.

The problem is that the above code is displaying all the "Job Disciplines" which have at least one post and not the current post taxonomy.

How can I sort out this issue?

Caulk answered 19/3, 2013 at 14:54 Comment(2)
Screenshot of the issue: docs.google.com/file/d/0BwVSqH5yzcN-Wm1YY29CdUxfbkk/editCaulk
You'll get a better answer for this at wordpress.stackexchange.com.Univalve
H
29

How to display current post taxonomies and terms

Here is a modified code from the Codex (see link below) that will display all the taxonomies of the current post with attached terms:

<?php 
// get taxonomies terms links
function custom_taxonomies_terms_links() {
    global $post, $post_id;
    // 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);
    $out = "<ul>";
    foreach ($taxonomies as $taxonomy) {        
        $out .= "<li>".$taxonomy.": ";
        // get the terms related to post
        $terms = get_the_terms( $post->ID, $taxonomy );
        if ( !empty( $terms ) ) {
            foreach ( $terms as $term )
                $out .= '<a href="' .get_term_link($term->slug, $taxonomy) .'">'.$term->name.'</a> ';
        }
        $out .= "</li>";
    }
    $out .= "</ul>";
    return $out;
} ?>

This is used like this:

    <?php echo custom_taxonomies_terms_links();?>

Demo output

The output might look like this if the current post has the taxonomies country and city:

<ul>
    <li>  country:  
          <a href="http://example.com/country/denmark/">Denmark</a> 
          <a href="http://example.com/country/russia/">Russia</a> 
    </li> 
    <li>   city:  
           <a href="http://example.com/city/copenhagen/">Copenhagen</a> 
           <a href="http://example.com/city/moscow/">Moscow</a> 
    </li> 
</ul>

Reference

The original code example in the Codex:

http://codex.wordpress.org/Function_Reference/get_the_terms#Get_terms_for_all_custom_taxonomies

Hope this helps - I'm sure you can adapt this to your project ;-)

Update

But what if I want to display only some of them and not all of them? Also, I would like to name them myself instead of it giving taxonomy names with underscores. Any idea how can I achieve that?

Here is one modification to achieve that:

function custom_taxonomies_terms_links() {
    global $post;
    // some custom taxonomies:
    $taxonomies = array( 
                         "country"=>"My Countries: ",
                         "city"=>"My cities: " 
                  );
    $out = "<ul>";
    foreach ($taxonomies as $tax => $taxname) {     
        $out .= "<li>";
        $out .= $taxname;
        // get the terms related to post
        $terms = get_the_terms( $post->ID, $tax );
        if ( !empty( $terms ) ) {
            foreach ( $terms as $term )
                $out .= '<a href="' .get_term_link($term->slug, $tax) .'">'.$term->name.'</a> ';
        }
        $out .= "</li>";
    }
    $out .= "</ul>";
    return $out;
} 
Heteroclite answered 19/3, 2013 at 16:30 Comment(4)
Lovely! This is the result screenshot: docs.google.com/file/d/0BwVSqH5yzcN-d3dSLVdPeUpPUUk/edit But what if I want to display only some of them and not all of them? Also, I would like to name them myself instead of it giving taxonomy names with underscores. Any idea how can I achieve that?Caulk
Here's an updated version that hides unassigned taxonomies: wordpress.stackexchange.com/questions/130993/…Heteroclite
Thanks! The really important nugget for me was: get_the_terms( $post->ID, $taxonomy ); I previously just had get_terms($taxonomy) which was incorrect.Toadeater
Glad it helped @tkmHeteroclite
C
2

Just in case someone wants to display them grouped by parent.

It's basically the same answer as above. I used this answer form another post: https://stackoverflow.com/a/12144671 to group them (by id and by parent).

Function modified to use it with objects:

function object_group_assoc($array, $key) {
    $return = array();
    foreach($array as $object) {
        $return[$object->$key][] = $object;
    }
    return $return;
}

Final function:

// get taxonomies terms links
function custom_taxonomies_terms_links() {
    global $post, $post_id;
    // 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);

    $out = "<ul>";
    foreach ($taxonomies as $taxonomy) {
        $out .= "<li>".$taxonomy.": ";
        // get the terms related to post
        $terms = get_the_terms( $post->ID, $taxonomy );

        if ( !empty( $terms ) ) {
            $terms_by_id = object_group_assoc($terms, 'term_id');
            $terms_by_parent = object_group_assoc($terms, 'parent');
            krsort($terms_by_parent);

            foreach ( $terms_by_parent as $parent_id => $children_terms ){

                if($parent_id != 0){//Childs
                    //Add parent to out string
                    $parent_term = $terms_by_id[$parent_id][0]; //[0] because object_group_assoc return each element in an array

                    $out .= '<li><a href="' .get_term_link($parent_term->slug, $taxonomy) .'">'.$parent_term->name.'</a>';

                    //Add children to out string
                    $out .= '<ul>';
                    foreach ($children_terms as $child_term) {

                        $out .= '<li><a href="' .get_term_link($child_term->slug, $taxonomy) .'">'.$child_term->name.'</a></li>';
                    }
                    $out .= '</ul></li>';

                } else {//parent_id == 0

                    foreach ($children_terms as $child_term) {
                        if(!array_key_exists($child_term->term_id, $terms_by_parent)){//Not displayed yet becouse it doesn't has children
                            $out .= '<li><a href="' .get_term_link($child_term->slug, $taxonomy) .'">'.$child_term->name.'</a></li>';
                        }

                    }
                    $out .= '</ul></li>';
                }
            }

        }
        $out .= "</li>";
    }
    $out .= "</ul>";
    return $out;
}

Used the same way:

<?php echo custom_taxonomies_terms_links();?>

Note: just working with one level children terms.

Chios answered 17/3, 2015 at 12:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.