就像标题所说,我试图只显示媒体库中特定类别下的项目。他们是否依附于任何东西。
目前我可以获取所有图片,但我不确定如何将其缩小到某些类别。
这是我到目前为止所拥有的:
<select name="event-dropdown" onchange="document.location.href=this.options[this.selectedIndex].value;">
<option value=""><?php echo esc_attr(__('Select Event')); ?></option>
<?php
$args = array(
'hide_empty' => 0,
);
$categories = get_categories($args);
foreach ($categories as $category) {
$option = '<option value="?cat='.get_cat_ID($category->cat_name).'">';
$option .= $category->cat_name;
$option .= ' ('.$category->category_count.')';
$option .= '</option>';
echo $option;
}
?>
</select>
<?php
$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);
if($_GET['cat']){
// not sure what to do here yet
}else{
// this part works fine
foreach ( $query_images->posts as $image) {
echo wp_get_attachment_image($image->ID);
}
}
?>
有人可以告诉我如何做到这一点。我能找到的只是与附加图像或后期图像相关的内容。我只是想直接从图书馆中取出它们。
编辑标签也可以使用。它不必是类别。
答案 0 :(得分:0)
除非您为媒体库中的每个图片添加自定义元标记,否则您的查询无法正常工作。
可能的解决方案 例如创建一个帖子,将其命名为相关内容,标记您希望与此帖子相关联的所有类别,现在将您要显示的所有图像转储到此帖子中。
您可以使用此查询来突出帖子&amp;您要显示的类别字词,以及附加到帖子的图像。
<?php
$args = array (
'post_type' => 'post',
'tax_query' => array(
array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => 'your-cat-name-here'
)
)
);
$custom_query = new WP_Query( $args );
if ( $custom_query->have_posts() ):
while ( $custom_query->have_posts() ) :
$custom_query->the_post();
// Do stuff with the post content.
$attachments = get_posts( array(
'post_type' => 'attachment',
'posts_per_page' => -1,
'post_parent' => $post->ID,
'exclude' => get_post_thumbnail_id()
) );
if ( $attachments ) {
foreach ( $attachments as $attachment ) {
$class = "post-attachment mime-" . sanitize_title( $attachment->post_mime_type );
$thumbimg = wp_get_attachment_link( $attachment->ID, 'thumbnail-size', true );
$image_title = $attachment->post_title;
$caption = $attachment->post_excerpt;
$description = $image->post_content;
//print_r($attachment);
echo '<div class="thumbnail"><a href="'.wp_get_attachment_url($attachment->ID).'" title="'.$attachment->post_excerpt.'"><figure><img src="'.wp_get_attachment_url($attachment->ID).'" /></figure></a></div>';
}
//End
echo '</div>';
}
endwhile;
else:
// we can insert something if Nothing found.
echo "<h2>Sorry, but there's nothing here.</h2>";
endif;
wp_reset_query();
?>