I am using the following to assign categories to my WordPress Media Library (in functions.php
):
function wptp_add_categories_to_attachments() {
register_taxonomy_for_object_type( 'category', 'attachment' );
}
add_action( 'init' , 'wptp_add_categories_to_attachments' );
Each Media Library item is assigned specific categories...I have some code that is returning the latest 5 images from the Media Library which looks like (in footer.php
):
<?php
$args = array(
'post_type' => 'attachment',
'post_mime_type' => 'image',
'orderby' => 'post_date',
'order' => 'desc',
'posts_per_page' => '5',
'post_status' => 'inherit'
);
query_posts($args);
?>
<?php if ( have_posts() ) : ?>
<div class="wrap">
<h5><span>Recently</span> Added</h5>
<ul>
<?php
while ( have_posts() ) : the_post();
$url = get_attachment_link(get_post_thumbnail_id());
$image = wp_get_attachment_image_src(get_the_ID(), "thumbnail");
?>
<li><a href="<?php echo $url; ?>"><img src="<?php echo $image[0]; ?>"></a></li>
<?php endwhile; ?>
</ul>
</div>
<?php else : ?>
<div class="wrap">
<h5>Oops...</h5>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
</div>
<?php endif; ?>
<?php wp_reset_query(); ?>
What I'd like to do is only return the images if they have an assigned category.
Any help is appreciated.
Thanks,
Josh