WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am using this function to display categories specified to a post having gallery on the image attachment page.

<p>CATEGORY: <?php the_category(', '); ?></p>

The problem is that this function doesn't return anything. I have also used this way to display categories but no success:

    <?php $categories = get_the_category();
          $separator = ', ';
          $output = ''; 
          if($categories){ ?>
    <?php foreach($categories as $category) {
    $output .= '<a href="'.get_category_link($category->term_id ).'" title="' . esc_attr( sprintf( __( "View all posts in %s" ), $category->name ) ) . '">'.$category->cat_name.'</a>'.$separator;
    echo trim($output, $separator);

Can anyone tell where problem lies???


share|improve this question
Are you sure you have Categories enabled for your Medialibrary? – fischi Dec 27 '12 at 11:53
Where I can find that?? – 10wtaylor Dec 27 '12 at 12:03
You can try a Plugin like this one – fischi Dec 27 '12 at 12:17
thanks fischi but I want to do this without using plugins. – 10wtaylor Dec 27 '12 at 12:19
Out of the Box, WordPress does not support Categories for your Media Library. You will have to use or make a Plugin for this, and putting it into your functions.php is not recommended as this is not a Themespecific feature. – fischi Dec 27 '12 at 12:20
up vote 1 down vote accepted

If you have WordPress 3.5 this will work.


First you need to enable this in your theme, Put this in your functions.php file in your theme root.

add_action('init', 'wpse_77390_enable_media_categories' , 1);
function wpse_77390_enable_media_categories() {
   register_taxonomy_for_object_type('category', 'attachment');

In your image.php or attachments.php file add:

$tax = get_the_term_list( $post->ID, 'category' );
echo $tax;

Then go and add some categories to a attachment.

share|improve this answer
Thanks Pontus, its working but I want to show parent post category on image.php, I have tried mostly all ways. Can you tell where problem lies?? – 10wtaylor Dec 28 '12 at 15:16

See the question / answer on this link. The answer have 2 different solutions, easy with default categories and taxonomies and also a solution with custom taxonomies only for media.

share|improve this answer
Bueltge, what I am trying to say is I want to show parent post category on attachment page here is the link, check this out as it is currently showing:i.imgur.com/QNCw9.jpg I can't individually add categories and taxonomies to all images, as my single post will consist of 50-70 photos as gallery.Is there a way, attachment images can fetch categories from parent post where it is uploaded as:i.imgur.com/6wrrw.jpg, or just able to show parent post category directly using code in image.php . – 10wtaylor Dec 29 '12 at 7:38
Thanks for the additional information, now it is clearly for me. – bueltge Dec 29 '12 at 22:05

Your Answer


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.