我试图获取特定术语的附件(在其存档页面中)。 但结果显示的结果图像是5次而不是1次。
我在此页面中有多个循环 - 一个用于显示相关帖子,另一个用于显示相关产品(自定义帖子),另一个用于显示相关图像。自定义帖子和帖子工作得很好,但我无法以正确的方式显示附件。 :S
<?php $queried_object = get_queried_object();
$term_id = $queried_object->term_id;
$args = array(
'post_status' => 'inherit',
'numberposts' => 0,
'post__not_in' => array_merge($do_not_duplicate,get_option( 'sticky_posts' )),
'post_type' => 'attachment',
);
$args['tax_query'] = array(
array(
'taxonomy' => 't-arte',
'terms' => $term_id,
'field' => 'id',
),
); ?>
<?php $t = $data['t-arte'];
$array = explode(" ", $t);
$array = array_unique($array);?>
<?php $media_query = array_unique($array); ?>
<?php $media_query = get_posts($args);
if( !empty( $media_query ) ) :
foreach ($media_query as $media_query) :
global $post; $post = $media_query;
setup_postdata($media_query);
?>
<div id="archivespage-media-item">
<?php $attachments = get_posts( $args );
if ( $attachments ) {
foreach ( $attachments as $attachment ) {
echo '<div id="imagem">';
the_attachment_link( $attachment->ID, true );
echo '</div>';
}
}?>
</div>
<?php endforeach;else :?>
<p>Ainda não temos nenhuma imagem relacionada :(</p>
</div>
<?php endif; ?>
<?php wp_reset_query();?>'
答案 0 :(得分:0)
我已经完成了这些并且正在工作!
结果将在术语的存档页面中显示特定术语中的所有附件。
<?php $queried_object = get_queried_object();
$term_id = $queried_object->term_id;
global $wp_query;
$original_query['tax_query'] = array(
array(
'taxonomy' => 't-arte',
'terms' => $term_id,
'field' => 'id',
),);
$original_query = (array) $wp_query;
$attach_query = array(
'post_type'=> array( 'attachment' ),
'post_status' => array( null ));
$args = array_merge($original_query['query_vars'], $attach_query);
$media_query = new WP_Query( $args )?>
<?php if($media_query->have_posts()) :
while ($media_query->have_posts() ) : $media_query->the_post();
if( $post->ID == $do_not_duplicate ) continue; ?>
<div id="archivespage-media-item">
<div id="imagem">
<?php echo wp_get_attachment_link($attachment->ID, 'bigger-thumb');?>
</div>
</div>
<?php endwhile; else: ?>
//do stuff
</div>