Support » Fixing WordPress » Term Description for custom taxonomies

  • Hi experts,

    I want to display description for custom taxonomies, I have a taxonomy called region and want to display its content.

    I am using this

    <?php echo term_description(”,’region’); ?>

    But not able to get any output.
    I have set the description for all regions in region taxonomy from WordPress admin panel UI

Viewing 6 replies - 1 through 6 (of 6 total)
  • According to the documentation, you need to pass in a valid term ID first.

    http://codex.wordpress.org/Function_Reference/term_description

    If you have the object for the term (I.e. you have already done get_terms() or similar) then just do echo $term->description on the term that you require. That will save an aditional query, so save a little bit of laod time.

    Thread Starter Mohit Kumar

    (@mohitkr05)

    I tried the same thing but it is not working

    <?php the_terms( $post->ID, ‘region’, ‘Region: ‘, ‘, ‘, ‘ ‘ ); ?>
    <?php echo $term->description ?>

    Try this, using only one of the looping methods (which ever suits you best). And please ignore any spelling mistakes, it’s nearly 5pm on a Friday!!!

    Also, take a look at the Function Reference for ‘get_terms()’ – it’s a very powerful function, and using the optional args() paramiter, you could find you are able to extract only the terms that you want fromt the DB there and then, meaning the first looping method below will suffice.

    <?php
    /** Get all of the terms from the taxonomy 'Region' */
    $taxonomies = 'region'
    $terms = get_terms($taxonomies)
    
    /** Loop through each term, outputing it's description */
    if(!empty($terms)) : foreach($terms as $term) :
            echo $term->description;
        endforeach;
    endif;
    
    /** Loop through each term, outputing only your desired terms by testing the descritpion matches */
    $descriptions = array('My description', 'Another description if you wish', 'and so on...');
    if(!empty($terms)) : foreach($terms as $term) :
            echo (in_array($term->description, $descriptions)) ? $term->description : false;
        endforeach;
    endif;
    ?>
    Thread Starter Mohit Kumar

    (@mohitkr05)

    how can i get current term id?
    <?php the_terms( $post->ID, ‘region’, ‘Region: ‘, ‘, ‘, ‘ ‘ ); ?>
    I have only 1 term related with any post, so i dont intend to loop it.

    If your term has the same name as your post, then you could use get_term_by('name', $post->post_name, 'Region');.

    If it is not the same name, but you will always have the same term associated with a post, then you could create a lookup function and use get_term_by('name', lookup_term_name($post->ID), 'Region');

    function lookup_term_name($post_id){
    
        $term_post_relationship = array(
            '1234' => 'term-1',
            '5678' => 'term-2',
            '8900' => 'term-3'
        );
    
        return (isset($term_post_relationship[$post_id])) ? $term_post_relationship[$post_id] : false;
    
    }
Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Term Description for custom taxonomies’ is closed to new replies.