categories on attachment page

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:

Read More
    <?php $categories = get_the_category();
          $separator = ', ';
          $output = ''; 
          if($categories){ ?>
          <span>CATERGORY:</span>
    <?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???

Thanks

Related posts

Leave a Reply

2 comments

  1. If you have WordPress 3.5 this will work.

    http://make.wordpress.org/core/2012/12/12/attachment-editing-now-with-full-post-edit-ui/

    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.

  2. 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.