WordPress.org

Ready to get started?Download WordPress

Forums

Term Description for custom taxonomies (7 posts)

  1. Mohit Kumar
    Member
    Posted 1 year ago #

    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

  2. Jonathan Dingman
    Member
    Posted 1 year ago #

    According to the documentation, you need to pass in a valid term ID first.

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

  3. duck__boy
    Member
    Posted 1 year ago #

    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.

  4. Mohit Kumar
    Member
    Posted 1 year ago #

    I tried the same thing but it is not working

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

  5. duck__boy
    Member
    Posted 1 year ago #

    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;
    ?>
  6. Mohit Kumar
    Member
    Posted 1 year ago #

    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.

  7. duck__boy
    Member
    Posted 1 year ago #

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

Topic Closed

This topic has been closed to new replies.

About this Topic