I use this code to show post tag(s) on single.php template:

<?php the_tags('<div>Tagged with:<br> ',' | ','</div>');?>

I wish to display post tags on attachment page too, but code above shows nothing on attachment (image.php) template.

How to display parent post tag(s) on attachment page (specially on attachment page of post featured image)?

2 Answers 2


This should work to get the parent's tags.

global $wp_query;
$attachment_id = $wp_query->post->ID;
$parent_id = get_post_field('post_parent', $attachment_id);
$parent_tags = wp_get_post_tags($parent_id); 
$tag_count = count($parent_tags); // Counting the tags to find know the last one so there is no pipe
$i = 1; // Setting up the count

if ($parent_tags) { ?>
    Tagged with:<br />
    <?php foreach ($parent_tags as $tag) {
        if($i < $tag_count) {
            echo '<a href=' . get_tag_link($tag->term_id) . '>' . $tag->name . '</a> | ';
        } else {
            echo '<a href=' . get_tag_link($tag->term_id) . '>' . $tag->name . '</a>';  
    } ?>
} ?>

Attachments are a separate post type in WordPress, so on theory if you want to specify tags for each uploaded file, you'll have to use this in your active theme's functions.php:

register_taxonomy( 'post_tag', 'attachment', $args );

For $args have a look here:


  • I do not wish to "...specify tags for each uploaded file...". I wish to display tags of parrent post on attachment page (attachment page of post featured image). Dec 26, 2014 at 20:53
  • Sorry, seems I got it all wrong there :)
    – vlood
    Dec 26, 2014 at 22:07

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.