WordPress.org

Forums

[resolved] Taxonomy term descriptions in below post content (9 posts)

  1. jvinch
    Member
    Posted 5 years ago #

    Folks,

    I am trying to show a term description at the bottom of a post. The term is part of a custom taxonomy.

    For instance, a post is assigned to the term Apples. In single.php, I'm listing the_content, to be followed by the description of the term this specific post is assigned to.

    I'm using

    <?php echo term_description( '', get_query_var( 'taxonomy' ) ); ?>

    for taxonomy.php - but this obviously won't work for single.php.

    What snippet could I use to:

    Display the term_description for term to which this post is assigned? I know how to get the term list with

    <?php echo get_the_term_list( $post->ID, 'taxonomy-name', '', ', ', '' ); ?>

    but I'm drawing a blank for the description.

    Thanks in advance for your help

    John

  2. jvinch
    Member
    Posted 5 years ago #

    Thought I'd give this one a bump. Anyone?

  3. MichaelH
    Member
    Posted 5 years ago #

    Use this

    $taxonomy='your_taxonomy_goes_here';
    $terms=get_the_terms($post->ID,$taxonomy);
    echo "<pre>"; print_r($terms); echo "</pre>";
  4. jvinch
    Member
    Posted 5 years ago #

    Michael - that's getting me somewhere, but not where I need to be yet.

    Here's what it prints:

    `Array
    (
    [774] => stdClass Object
    (
    [term_id] => 774
    [name] => The Name
    [slug] => the-name
    [term_group] => 0
    [term_taxonomy_id] => 826
    [taxonomy] => the taxonomy
    [description] => the description the description the description etc
    [parent] => 0
    [count] => 16
    [object_id] => 1461
    )

    )
    '

    We're probably extremely close, I just don't know how to adjust the above to just print the term description.

  5. Mark / t31os
    Moderator
    Posted 5 years ago #

    $terms = get_the_terms( $post->ID , 'taxonomy-name_here' );
    echo $terms->description;
  6. jvinch
    Member
    Posted 5 years ago #

    t31os_ - that does nothing for me. No result.

    Just to make sure we're all on the same page - I am trying to print a term description at the top or bottom of single.php, not on taxonomy pages.

  7. Mark / t31os
    Moderator
    Posted 5 years ago #

    $terms = get_the_terms( $post->ID , 'taxonomy_Name' );
    if($terms) {
    	foreach( $terms as $term ) {
    		echo $term->description.'<br />';
    	}
    }

    Just need to loop over results... :)

  8. jvinch
    Member
    Posted 5 years ago #

    Bingo - spelling of description threw me for a loop in the last snippet, but works like a champ now.

    For anyone else -

    <?php $terms = get_the_terms( $post->ID , 'taxonomy-name' );
    if($terms) {
    	foreach( $terms as $term ) {
    		echo $term->description.'<br />';
    	}
    }
    ?>

    Thanks much, Michael and t31os_!

  9. Designophobia
    Member
    Posted 5 years ago #

    I have the following code:

    <?php $terms = get_the_terms( $post->ID , 'people' );
    	if($terms) {
    		foreach( $terms as $term ) {
    		echo '<div class="authorbio">'.$term->description.'<br /></div>';
    	}
    }
    ?>

    I only have descriptions on certain custom tags. I don't want to display the "authorbio" divs when the description is missing. How do I do it?

Topic Closed

This topic has been closed to new replies.

About this Topic