If tag exists, then echo once

时间:2013-06-07 作者:Austin Biggs

我有以下代码,我试图检查是否存在特定的标记,如果存在,我需要回显一次。问题是,当标签存在于多个地方时,它会重复多次。

// LOOP ARGUMENTS
$args = array(
    \'post_type\' => \'xyz_members\', 
    \'posts_per_page\' => -1, 
    \'orderby\' => \'title\', 
    \'order\' => \'ASC\', 
    \'tax_query\' => array(
        array(
            \'taxonomy\' => \'member_types\',
            \'field\' => \'slug\',
            \'terms\' => \'current-class\',
            \'operator\' => \'NOT IN\'
        )
    )
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();                              
    // CUSTOM CONTENT
    $postTags = get_the_tags();
    if ($postTags) while($postTags as $tag) {
        if ($tag->slug == \'2006\') { ?><li id="_2013" class="filter on">2006</li><?php }
        if ($tag->slug == \'2007\') { ?><li id="_2013" class="filter on">2007</li><?php }
        if ($tag->slug == \'2008\') { ?><li id="_2013" class="filter on">2008</li><?php }
        if ($tag->slug == \'2009\') { ?><li id="_2013" class="filter on">2009</li><?php }
        if ($tag->slug == \'2010\') { ?><li id="_2013" class="filter on">2010</li><?php }
        if ($tag->slug == \'2011\') { ?><li id="_2013" class="filter on">2011</li><?php }
        if ($tag->slug == \'2012\') { ?><li id="_2013" class="filter on">2012</li><?php }
        if ($tag->slug == \'2013\') { ?><li id="_2013" class="filter on">2013</li><?php }
        if ($tag->slug == \'2014\') { ?><li id="_2013" class="filter on">2014</li><?php }
        if ($tag->slug == \'2015\') { ?><li id="_2013" class="filter on">2015</li><?php }
        if ($tag->slug == \'2016\') { ?><li id="_2013" class="filter on">2016</li><?php }
        if ($tag->slug == \'2017\') { ?><li id="_2013" class="filter on">2017</li><?php }
        if ($tag->slug == \'2018\') { ?><li id="_2013" class="filter on">2018</li><?php }
        if ($tag->slug == \'2019\') { ?><li id="_2013" class="filter on">2019</li><?php }
        if ($tag->slug == \'2020\') { ?><li id="_2013" class="filter on">2020</li><?php }
    }
/* END WHILE AND RESET QUERY */ 
endwhile; wp_reset_query();

1 个回复
SO网友:birgire

我不知道你想要什么,但如果你想得到一个独特的<li>-可以尝试的查询列表(未测试):

<?php

// LOOP ARGUMENTS
$args = array( \'post_type\' => \'xyz_members\', \'posts_per_page\' => -1, \'orderby\' => \'title\', \'order\' => \'ASC\', \'tax_query\' => array(array(\'taxonomy\' => \'member_types\',\'field\' => \'slug\',\'terms\' => \'current-class\',\'operator\' => \'NOT IN\')));
$loop = new WP_Query( $args );
$tags = array();

// COLLECT 
while ( $loop->have_posts() ) : $loop->the_post();
    $postTags = get_the_tags();
    if ($postTags):
        while($postTags as $tag):
            $tags[$tag->slug] = $tag->slug; 
        endwhile;
    endif;
endwhile; 

// OUTPUT
$years = range( 2006, 2020 );
foreach( $years as $year ):
    $year = (string) $year;
    if( isset( $tags[$year] ) )
         printf(\'<li id="_%s" class="filter on">%s</li>\', $tags[$year], $tags[$year] );

endforeach;

?>
你也可以考虑array_unique() 从数组中删除重复值。

结束