0

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

0

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.

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.