1

I've got a shortcode working to return tags that are used on a particular post but can't figure out how to return the description with it.

function returnpost_tags(){
       return get_the_tag_list('',', ',' ');
   }
add_shortcode('post-tags', 'returnpost_tags');

I tried

return get_the_tag_list('',', ',' $description'); 

but obviously something not right

as a bonus I'd like to alternatively as a separate function return the list with description but without the links to the tags archive which is the normal behavior

thanks for the help

2
  • This question is already asked here stackoverflow.com/questions/25499308/…, I think you can't use get_the_tag_list(), you have to try with get_terms() .
    – Ronald
    Commented Jun 17, 2017 at 3:56
  • thanks @Ronald but I can't even begin to decipher how that thread would address this - it looks like completely different scenario Commented Jun 17, 2017 at 4:12

2 Answers 2

1

This is how you can loop with custom taxonomy.

function returnpost_tags(){
// get tags by post ID 
$post_ID = get_the_ID();
// here, you can add any custom tag
$terms = get_the_terms( $post_ID , 'post_tag' ); 
echo '<ul>';

foreach ( $terms as $term ) {

// The $term is an object, so we don't need to specify the $taxonomy.
$term_link = get_term_link( $term );
$term_ID = $term->term_id;
// If there was an error, continue to the next term.
if ( is_wp_error( $term_link ) ) {
    continue;
}

echo '<li><a href="' . esc_url( $term_link ) . '">' . $term->name . '</a></li>';
echo term_description($term_ID);

   // another option
   // echo  '<p>' . $term->description . '</p>';

}

echo '</ul>';
}
add_shortcode('post-tags', 'returnpost_tags');

You can replace post_tag with your custom taxonomy

$terms = get_terms( 'post_tag' ); 

To get tag's description, you can use one of the below method.

 echo $term->description;

or

 echo term_description($term->term_id);
5
  • Hi @Ronald - the output works well, however it is getting ALL the tags and not just the tags associated with the specific post. I tried a couple alternatives for $terms = get_terms( 'post_tag' ); but just don't know what I'm doing. Commented Jun 17, 2017 at 13:11
  • I thought you want to print out all the tags. To get the tags which are associated with the specific post, you need to add the post ID. It would be like this $terms = get_the_terms( $post->ID , 'post_tag' ); . I've updated my answer with new codes and hope it help you.
    – Ronald
    Commented Jun 17, 2017 at 17:15
  • Thanks @Ronald, just what I was hoping for - it was the piece about // get tags by post ID $post_ID = get_the_ID(); that was the missing element for me. BTW I'm getting PHP error warnings associated with ** Invalid argument supplied for foreach() ** -- which reading about elsewhere apparently has something to do with needing an array --- but I can't quite figure out how to change that.... there is a waterfall effect with this error -- meaning about 10 other pieces connected to each other about what gets displayed are flagged as invalid, so not exactly trivial I don't think. Commented Jun 18, 2017 at 6:00
  • ** Invalid argument supplied for foreach() ** --- I can't seem to figure out how to use an array for this, any ideas Commented Jun 21, 2017 at 6:18
  • FYI - I was getting display problems because the term definitions were appearing at the top of the post no matter what I did with html, however making this change combining ECHO and RETURN - solved the issue echo '<div class="absolute"><p style="margin:0;"><strong>' . $term->name . '</strong></p></div>'; return term_description( $term->term_id, 'post_tag' ); Commented Jun 21, 2017 at 9:31
0

If you are trying to work with the tags, you can use get_the_tags() in the loop instead:

function my_tag_shortcode(){
    // Get a list of tags
    $tags = get_the_tags();
    $data ='';
    // Run a loop to output the data
    foreach ($tags as $tag) { 
        $data .= '<a href="'.get_tag_link($tag->term_id).'">'. $tag->name .'</a><span>'. $tag->description .'</span>';
    }
    return $data;
}

Now, you have the tag's name, ID, description, and more. Choose whatever you want, based on your needs.

1
  • thank @Jack Johansson -- 'return $data' needs to be 'return $data;' but it wprked returning what I need Commented Jun 17, 2017 at 7:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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