Categories for Attachments

Categories

1
2
3
4
function wptp_add_categories_to_attachments() {
    register_taxonomy_for_object_type( 'category', 'attachment' );
}
add_action( 'init' , 'wptp_add_categories_to_attachments' );

To Apply It Use “get_posts”

1
2
3
4
5
6
7
8
9
10
11
12
13
14
$call_args = array(
    'post_type'        => 'attachment',
    'cat'              => 5,
    'posts_per_page'   => -1
);
 
$attachments = get_posts($call_args);
 
if (!empty($attachments)) { foreach($attachments as $attachment) {
     
    $src = $attachment->guid;
    echo "<img src='$src' />";
 
}}

Tags

1
2
3
4
5
// apply tags to attachments
function wptp_add_tags_to_attachments() {
    register_taxonomy_for_object_type( 'post_tag', 'attachment' );
}
add_action( 'init' , 'wptp_add_tags_to_attachments' );
Comments