So I have this function code that adds "Category" to the Images that I upload in my Wordpress website.
/** Register taxonomy for images */
function olab_register_taxonomy_for_images() {
register_taxonomy_for_object_type( 'category', 'attachment' );
}
add_action( 'init', 'olab_register_taxonomy_for_images' );
/** Add a category filter to images */
function olab_add_image_category_filter() {
$screen = get_current_screen();
if ( 'upload' == $screen->id ) {
$dropdown_options = array( 'show_option_all' => __( 'View all categories', 'olab' ), 'hide_empty' => false, 'hierarchical' => true, 'orderby' => 'name', );
wp_dropdown_categories( $dropdown_options );
}
}
add_action( 'restrict_manage_posts', 'olab_add_image_category_filter' );
I'd like to know how can I call or display all the Images that falls under a specific category (the category number that I want to call is Category # 2190)?
What I'm trying to do here is to have a photo gallery that showcases all the photos i've uploaded and tagged under the category #2190 - "Photo of the day"?
You can organize media categories via admin just like you control post categories. The plugin also allows you to sort media items in your WordPress media library using taxonomy in both list and grid views. While uploading media files, you can choose a default category.
Where are your media files stored? Your media files are uploaded to the /wp-content/uploads/folder. Normally, the image is also placed inside a folder the month it was uploaded.
The following code should do what you are trying to achieve
<?php
$images = get_posts( array('post_type' => 'attachment', 'category__in' => array(2190)) );
if ( !empty($images) ) {
foreach ( $images as $image ) {
echo wp_get_attachment_image($image->ID).'<br />';
echo $image->post_title .'<br />';
the_attachment_link( $image->ID, true );
}
}
?>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With