简体   繁体   English

如何在Wordpress中的特定类别下显示图像(来自媒体库)?

[英]How to display the Images (from Media Library) under a specific Category in Wordpress?

So I have this function code that adds "Category" to the Images that I upload in my Wordpress website. 所以我有这个功能代码,将“类别”添加到我在Wordpress网站上传的图像中。

/** 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)? 我想知道如何调用或显示属于特定类别的所有图像(我要调用的类别编号是#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"? 我在这里要做的是有一个照片库,展示我上传的所有照片,并标记为#2190类别 - “当天的照片”?

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 );
    }
}
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM