分类存档+查询附件=重复结果

时间:2014-04-07 作者:Bibiana

我正在尝试获取特定术语的附件(在其存档页中)。但是结果显示了5次结果图像,而不是一次。

我在这个页面上有多个循环——一个用来显示相关帖子,另一个用来显示相关产品(自定义帖子),还有一个用来显示相关图片。自定义帖子和帖子运行良好,但我无法以正确的方式显示附件: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();?>

2 个回复
SO网友:Chip Bennett

我认为问题是你把错误的论点传给了$attachments 查询,导致您无法在$attachments 查询

以下是您正在执行的操作:

$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\',
    ),
);

$attachments = get_posts( $args );
因此,您正在查询所有属于帖子类型的帖子attachment, 而不仅仅是连接到当前帖子的帖子在$media_query.

这是你如何循环的$media_query:

foreach ($media_query as $media_query) :
(注意:格式错误。请尝试以下操作foreach ( $media_query as $media ) : 相反。)

您需要将当前帖子的ID传递给$attachments 查询,作为post_parent. 简单的可能是:

$attachments = get_posts( array(
    \'post_status\' => \'inherit\',
    \'post_type\' => \'attachment\',
    \'post_parent\' => $media_query->ID
    \'numberposts\' => 0
) );

SO网友:Bibiana

我明白了!结果将在术语的存档页面中显示特定术语中的所有附件。谢谢Chip!

<?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>

结束

相关推荐

自定义帖子类型(Jigoshop):实时服务器上$ategory_id(通过$Term->Term_Taxonomy_id)的意外值

我已经拼凑了一个函数,为Jigoshop自定义帖子类型及其相关图像提供类别描述。(product\\u cat image字段由Jigoshop插件实现)。它在本地工作,但在我的服务器上并不总是能找到图像-对于某些(但不是全部)产品,返回的$category\\u id似乎为1。function cyclelux_brand_info() { function get_the_category_bytax( $id = false, $tcat = \'category\' ) {