0

How can I call the name of an attachment category?

Something that would look like <?php get_attachment_category_name ?> or $attachment->category_name

This would simply output the name of the category, which was created using the below function:

function add_categories_to_attachments() {
    register_taxonomy_for_object_type( 'category', 'attachment' );
}
add_action( 'init' , 'add_categories_to_attachments' );
  • 1
    Where will you be calling it from? attachment.php template file? – Tim Malone May 12 '16 at 8:19
  • Just from the media library. I think this is close to what I need: <?php $attachment_data = wp_prepare_attachment_for_js( $attachment->ID ); echo '.get_cat_name($catid).'; ?> – BlueHelmet May 12 '16 at 17:29
0

The answer was found here: http://www.wprecipes.com/how-to-display-category-name-without-link

For my purposes, I needed to define the category function and then call it by name-only inside the php echo:

     <?php
$category = get_the_category($attachment->ID);
    echo 'html goes here';
    echo ''.$category[0]->cat_name.';
    ?>

Additionally, to help eliminate space or punctuation in the category name, it can display the slug instead:

<?php
$category = get_the_category($attachment->ID);
    echo 'html goes here';
    echo ''.$category[0]->slug.';
    ?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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