从自定义帖子类型循环中删除标记

时间:2015-04-30 11:01:46

标签: php html wordpress loops

我目前正在使用以下代码显示我的类别中的所有页面:

      <?php if (have_posts()) : ?>
      <?php while (have_posts()) : the_post(); ?>
      <a href="<?php echo get_permalink(); ?>">
       <?php echo "<div class='col-md-6' style='margin-bottom:20px;'>"; ?>
       <div class="row">
            <div class="col-md-6 col-sm-6 col-xs-12 nopr"><?php echo get_the_post_thumbnail( $page->ID, 'categoryimage', array('class' => 'testclass')); ?> </div>
            <div class="col-md-6 col-sm-6 col-xs-12 categorytiletext2">
                  <div class="testdiv">
                       <h4><?php the_title(); ?></h4>
                       <p><?php the_excerpt(); ?></p>
                 </div>
           </div>
     </div>
     <?php echo "</div>"; ?>

</a>
<!-- If there is no posts, display an error message -->
<?php endwhile; else: ?>
      <p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
<?php endif; ?>
<!-- If there is no posts, display an error message -->

这很有效,但我现在已经为这些页面添加了标签。我需要做的是从循环中删除某些标记。例如,我的标签叫做maintag。

我试图添加以下内容:

  <?php $my_query = new WP_Query(array('tag__not_in'=>array('maintag'), 'category_name'=>'Clients', 'orderby'=>'title', 'order'=>'ASC')); ?>

但这不起作用。

这是否需要由ID来完成?

编辑:

新代码,仍然无效?

    <?php if (have_posts()) : ?>
      <?php while (have_posts()) : the_post(); ?>
      <?php $tag_object = get_term_by('slug', 'sector1', 'post_tag');
$tagID = $tag_object->term_id;


$my_query = new WP_Query(array('tag__not_in'=> array($tagID), 'category_name'=>'Clients', 'orderby'=>'title', 'order'=>'ASC')); ?>
      <a href="<?php echo get_permalink(); ?>">
       <?php echo "<div class='col-md-6' style='margin-bottom:20px;'>"; ?>
       <div class="row">
            <div class="col-md-6 col-sm-6 col-xs-12 nopr"><?php echo get_the_post_thumbnail( $page->ID, 'categoryimage', array('class' => 'testclass')); ?> </div>
            <div class="col-md-6 col-sm-6 col-xs-12 categorytiletext2">
                  <div class="testdiv">
                       <h4><?php the_title(); ?></h4>
                       <p><?php the_excerpt(); ?></p>
                 </div>
           </div>
     </div>
     <?php echo "</div>"; ?>

</a>
<!-- If there is no posts, display an error message -->
<?php endwhile; else: ?>
      <p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
<?php endif; ?>
<!-- If there is no posts, display an error message -->

1 个答案:

答案 0 :(得分:0)

如果maintag为slug,则在第一个参数中使用'slug',如果它是name,则使用'name'代替slug

link:get_term_by()

$tag_object = get_term_by('slug', 'maintag', 'post_tag');
$tagID = $tag_object->term_id;


$my_query = new WP_Query(array('tag__not_in'=> array($tagID), 'category_name'=>'Clients', 'orderby'=>'title', 'order'=>'ASC'));