Page 1 of 1

retriving attachement category

Posted: Mon Feb 05, 2018 11:46 pm
by Ahmed Abdelbaky
good day admin
I have add category for the attachment by adding the following code to function.php
// add categories for attachments
function add_categories_for_attachments() {
    register_taxonomy_for_object_type( 'category', 'attachment' );
}
add_action( 'init' , 'add_categories_for_attachments' );
?>
the problem that when i try to show the attachment category it give me nothing ( i mean nothing to show up when i try to retrive attachment assigin in certain attachmment category ).
I need to show up ( retrive ) the attachment category ( as post category )
thank you

Re: retriving attachement category

Posted: Fri Feb 09, 2018 10:28 pm
by admin
Hi Ahmed,

Thanks for joining our forum :D
Your code should work just fine. Maybe the ?> at the end is what causing the error. Remove it.
As a rule of thumb, never close your PHP files unless there is a HTML code after the PHP code and always prefix your PHP functions.
This is a slightly better version of your code:
add_action( 'init' , 'redwaves_add_categories_for_attachments' );
if ( !function_exists( 'redwaves_add_categories_for_attachments' ) ) {
    function redwaves_add_categories_for_attachments() {
        register_taxonomy_for_object_type( 'category', 'attachment' );
    }
}