2013-12-10 75 views
0

我有这段代码在运行,为了获得帖子。如何不显示具有特定标签的帖子?

<?php if (have_posts()) : ?> 
<?php // The loop ?> 
<?php while (have_posts()) : the_post(); ?> 
<?php get_template_part('content', get_post_format()); ?> 
<?php endwhile; ?> 
<?php twentythirteen_paging_nav(); ?> 
<?php else : ?> 
<?php get_template_part('content', 'none'); ?> 
<?php endif; ?> 

我想只返回没有一些特定标签的帖子。 我该怎么办?

在此先感谢!

回答

0

您可以使用条件has_tag()功能:

<?php if (have_posts()) : ?> 
<?php // The loop ?> 
<?php while (have_posts()) : the_post(); ?> 
    <?php if(! has_tag('tag-name')) : ?> 
    <?php get_template_part('content', get_post_format()); ?> 
    <?php endif; ?> 
<?php endwhile; ?> 
<?php twentythirteen_paging_nav(); ?> 
<?php else : ?> 
<?php get_template_part('content', 'none'); ?> 
<?php endif; ?> 

注意has_tag()接受单个标签名称或标记的数组(所以如果有是您想要忽略的多个标签,您可以在上面的代码中使用if(has_tag(array('tag1', 'tag2', 'tag3',)))。另外,has_tag()只能在The Loop中使用。(更正度:http://wpseek.com/has_tag/,要求你在The Loop中只有WP版本2.7才有效。)

+0

你摇滚哥们!它不能更好地工作!非常感谢! –

+0

如果这适用于您,请将答案标记为** Accepted ** - 问题左侧应该有一个复选标记,以便您可以执行此操作。详细信息请参见[旅游](http://stackoverflow.com/tour)。 –

0
$args=array("tag__not_in"=>array(1,2,3)); 
$the_query = new WP_Query($args); ?> 

<?php if ($the_query->have_posts()) : ?> 

    <!-- the loop --> 
    <?php while ($the_query->have_posts()) : $the_query->the_post(); ?> 
    <h2><?php the_title(); ?></h2> 
    <?php endwhile; ?> 

    <?php wp_reset_postdata(); ?> 

只需通过标签ID

array("tag__not_in"=>array(1,2,3)); 
相关问题