我在这里需要做的是从媒体库中仅按其类别获取图像。它不会附在帖子上,也就是说,该图片不会作为任何帖子类型的“特色图片”上传。它将直接通过媒体库上传。然而,我创建了一个函数,允许我将自定义类别应用于这些图像。我成功地从媒体库中提取了以下图像:
$query_images_args = array(
\'post_type\' => \'attachment\',
\'post_mime_type\' =>\'image\',
\'post_status\' => \'inherit\',
\'posts_per_page\' => -1,
);
$query_images = new WP_Query( $query_images_args );
$images = array();
foreach ( $query_images->posts as $image) {
$images[]= wp_get_attachment_url( $image->ID );
}
这是我对类别的自定义函数:
function add_attachment_taxonomy() {
$labels = array(
\'name\' => \'Categories\',
\'singular_name\' => \'Category\',
\'search_items\' => \'Search Category\',
\'all_items\' => \'All Category\',
\'parent_item\' => \'Parent Category\',
\'parent_item_colon\' => \'Parent Category:\',
\'edit_item\' => \'Edit Category\',
\'update_item\' => \'Update Category\',
\'add_new_item\' => \'Add New Category\',
\'new_item_name\' => \'New Category Name\',
\'menu_name\' => \'Categories\',
);
$args = array(
\'labels\' => $labels,
\'hierarchical\' => true,
\'query_var\' => \'true\',
\'rewrite\' => \'true\',
\'show_admin_column\' => \'true\',
);
register_taxonomy( \'attachment_category\', \'attachment\', $args );
}
add_action( \'init\', \'add_attachment_taxonomy\' );
但是当我添加
\'category_name\' => term_slug
它仍然没有显示任何东西。我还需要做些什么来完成这项工作吗?
最合适的回答,由SO网友:Pieter Goosen 整理而成
\'category_name\'
不起作用。您使用的是自定义分类法,而不是内置类别。由此看来,您对分类法存在误解。请慢慢来看看this post 关于这个特定的主题
对于自定义分类,请使用tax_query
检索所需的帖子。以下是一个示例:
$query_images_args = array(
\'post_type\' => \'attachment\',
\'post_mime_type\' =>\'image\',
\'post_status\' => \'inherit\',
\'posts_per_page\' => -1,
\'tax_query\' => array(
array(
\'taxonomy\' => \'attachment_category\',
\'field\' => \'slug\',
\'terms\' => \'SLUG OF YOUR TERM\',
),
),
);
$query_images = new WP_Query( $query_images_args );