排除给定类别中的帖子

时间:2017-05-24 作者:Sdesign

我有一个查询,它从4种不同的自定义帖子类型中提取最新的帖子,这些类型都有一个与之关联的给定类别。我试图弄明白,如果最新的帖子有“探索”类别,即使它也有“户外娱乐”类别,我怎么能不显示出来。

<?php
$myquery[\'tax_query\'] = array(
\'relation\' => \'OR\',

 array(
    \'taxonomy\' => \'category\',
    \'category__not_in\' => array(\'explore\'),
    \'field\' => \'slug\',  
  ),
 array(
    \'taxonomy\' => \'category\',
    \'terms\' => array(\'outdoor-recreation\'),
    \'field\' => \'slug\',  
 ),
 array(
    \'taxonomy\' => \'take_category\',
    \'terms\' => array(\'outdoor-recreation\'),
    \'field\' => \'slug\',
),
 array(
    \'taxonomy\' => \'story_category\',
    \'terms\' => array(\'outdoor-recreation\'),
    \'field\' => \'slug\',
  ),
  array(
    \'taxonomy\' => \'news-category\',
    \'terms\' => array(\'outdoor-recreation\'),
    \'field\' => \'slug\',
  ),

  );
  $myquery[\'showposts\'] = 1;

  query_posts($myquery);

  while( $wp_query->have_posts() ) :
  $wp_query->the_post();

  ?>
  <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to           <?php the_title_attribute(); ?>">
  <?php  
 if ( has_post_thumbnail()):  
the_post_thumbnail( \'feed-fixed\' );  
endif;  ?></a>
 <h3><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link      to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h3>
 <?php echo wp_trim_words( get_the_excerpt(), 20 ); ?>
 <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to           <?php the_title_attribute(); ?>">>>More</a>
<?php endwhile;
wp_reset_query();
 ?>

1 个回复
SO网友:Sam

检查数组是否包含未经实时测试但有效的PHP类别

    <?php
    $myquery[\'tax_query\'] = array(
    \'relation\' => \'OR\',

      array(
        \'taxonomy\' => \'category\',
        \'category__not_in\' => array(\'explore\'),
        \'field\' => \'slug\',  
      ),
      array(
        \'taxonomy\' => \'category\',
        \'terms\' => array(\'outdoor-recreation\'),
        \'field\' => \'slug\',  
    ),
    array(
        \'taxonomy\' => \'take_category\',
        \'terms\' => array(\'outdoor-recreation\'),
        \'field\' => \'slug\',
    ),
     array(
        \'taxonomy\' => \'story_category\',
        \'terms\' => array(\'outdoor-recreation\'),
        \'field\' => \'slug\',
      ),
      array(
         \'taxonomy\' => \'news_category\',
        \'terms\' => array(\'outdoor-recreation\'),
        \'field\' => \'slug\',
       ),

     );
     $myquery[\'showposts\'] = 1;

     query_posts($myquery);

    while( $wp_query->have_posts() ) :
    $wp_query->the_post();

    // Get post id
    $post_id = get_the_ID();
    // Check categories of post
    $categories = get_the_category($post_id);
    if (in_array("explore", $categories)) {
      // explore is in the array
    }
    else{
     // Not in array do something 
    }
    endwhile;
  wp_reset_query();
     ?>

结束

相关推荐