Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm trying to use tags to create a list of cross references for each post and want the tag description to show next to the tag and/or as the title attribute for the hyperlink so the description shows when a visitor holds the cursor over the link.

Using the TwentyTen theme I have located the code in functions.php and amended it as below:

function twentyten_posted_in() {
    // Retrieves tag list of current post, separated by commas.
    $tag_list = get_the_tag_list( $before = '<br /> <br />' , $sep = ' here'.'<br /><br />' , $after = '<br />' );
    if ( $tag_list ) {
        $posted_in = __( 'Cross Reference %2$s. ', 'twentyten' );
    } 


    // Prints the string, replacing the placeholders.
    printf(
        $posted_in,
        get_the_category_list( ',  ' ),
        $tag_list,
                get_permalink(),
        the_title_attribute( 'echo=0' )
    );
}
endif;

As you can see I've changed the heading to say Cross Reference and by adding breaks I have a list of tags rather than a paragraph. I've identified where I want the Tag Description to go with the word "here".

I thought it would be as simple as adding tag_description() but that didn't work. If I put a number in the brackets, e.g. tag_description(5) it displays the tag description in the correct place, but of course all the tags have the wrong description.

Help!

Thanks Ian.

share|improve this question
add comment

1 Answer 1

Use get_the_terms() and create a custom array:

function wpse_31396_terms_with_desc( $post_id = NULL, $taxonomy = 'post_tag' )
{
    NULL === $post_id && $post_id = get_the_ID();

    if ( empty ( $post_id ) )
    {
        return '';
    }

    $terms = get_the_terms( $post_id, $taxonomy );

    if ( empty ( $terms ) )
    {
        return '';
    }

    $list = array ();


    foreach ( $terms as $term )
    {
        $list[ $term->term_id ] = array(
            'url' => get_term_link( $term, $taxonomy ),
            'name' => $term->name,
            'description' => $term->description
        );
    }

    return $list;
}

Sample usage for the functions.php:

add_filter( 'the_content', 'wpse_31396_add_terms_with_desc' );

function wpse_31396_add_terms_with_desc( $content )
{
    $tags = wpse_31396_terms_with_desc();

    if ( '' === $tags )
    {
        return $content;
    }

    $taglist = '<h2>Cross Reference</h2><ul class="taglist-with-desc">';
    foreach ( $tags as $tag )
    {
        $desc = empty ( $tag['description'] )
            ? '' : '<div class="tagdesc">' . wpautop( $tag['description'] ) . '</div>';
        $taglist .= sprintf(
            '<li><a href="%1$s">%2$s</a>%3$s</li>',
            $tag['url'],
            $tag['name'],
            $desc
        );
    }

    return $content . $taglist . '</ul>';
}

enter image description here

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.