Display current post custom taxonomy in WordPress

社会主义新天地 提交于 2019-12-20 09:45:24

问题


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?


回答1:


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;
} 



回答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.



来源:https://stackoverflow.com/questions/15502811/display-current-post-custom-taxonomy-in-wordpress

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!