I found a solution!
My code changed quite a bit…
Here is what the functions.php
file looks like (which did not change):
function wptp_add_categories_to_attachments() {
register_taxonomy_for_object_type( 'category', 'attachment' );
}
add_action( 'init' , 'wptp_add_categories_to_attachments' );
But here is the new code in footer.php
, which returns the images from the Media Library, but only from an assigned category:
<?php
$args = array(
'post_type' => 'attachment',
'numberposts' => '5',
'category_name' => 'your-category-name'
);
$images = get_posts($args);
if (!empty($images)) {
?>
<div class="wrap">
<h5><span>Recently</span> Added</h5>
<ul>
<?php
foreach ($images as $image) {
$attachment_link = get_attachment_link( $image->ID );
echo "<li><a href="".$attachment_link."">".wp_get_attachment_image($image->ID)."</a></li>";
}
?>
</ul>
</div>
<?php } else { ?>
<div class="wrap">
<h5>Oops...</h5>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
</div>
<?php } ?>
I could have tweaked my existing code my adding category_name => 'your-category-name'
to my $args
, but this is a much more simplified approach and does the exact same thing…it just uses get_posts
instead of query_posts
.