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){ ?>
<span>CATERGORY:</span>
<?php foreach($categories as $category) {
$output .= '<a href="'.get_category_link($category->term_id ).'" rel="nofollow noreferrer noopener" 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
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 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.
Method 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.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0