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

Kar*_*sca 6 php wordpress

所以我有这个功能代码,将"类别"添加到我在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' );
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

我想知道如何调用或显示属于特定类别的所有图像(我要调用的类别编号是#2190类别)?

我在这里要做的是建立一个照片库,展示我上传的所有照片,并标记为#2190类别 - "当天的照片"?

Ana*_*hah 4

以下代码应该可以实现您想要实现的目标

<?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 );
    }
}
?>
Run Code Online (Sandbox Code Playgroud)