如何结合这两个“如果”条件?

时间:2015-04-21 13:18:29

标签: php if-statement

通过这段代码,我得到了最后3篇包含" solar-signs-related"标签上的自定义帖子"产品"有" solar-signs-streetscape-elements"的页面术语。

但如何结合两个" if"因此,它还会显示与内部系统相关的博客文章"标签如果自定义帖子"产品"页面是"内部系统"?

if ( has_term( 'interior-systems', 'categories' ) ) {
  $args = array(
  'post_type' => 'post',
  'showposts' => 3,
  'post__not_in' => array( $post->ID ),
  'post_status' => array('publish'),
  'orderby' => 'date',
  'order' => 'DESC',
    'tax_query' => array(
     array(
    'taxonomy' => 'post_tag',
    'field' => 'slug',
    'terms' => array('interior-systems-related') )
    )
);
}
if ( has_term( 'solar-signs-streetscape-elements', 'categories' ) ) {
$args = array(
  'post_type' => 'post',
  'showposts' => 3,
  'post__not_in' => array( $post->ID ),
  'post_status' => array('publish'),
  'orderby' => 'date',
  'order' => 'DESC',
    'tax_query' => array(
     array(
      'taxonomy' => 'post_tag',
      'field' => 'slug',
      'terms' => array('solar-signs-related') )
   )
 );
 }
$query = new WP_Query($args);

if ($query->have_posts()) : while ($query->have_posts()) :       
$query->the_post(); ?>           
<div class="post-wrap relat relatblog">
<h2 class="entry-title"><?php the_title(); ?></h2>
</div>                   
  <?php endwhile;
  wp_reset_postdata();
  endif;
  endwhile; ?>

3 个答案:

答案 0 :(得分:0)

您可以尝试这样的事情:

$args = array(
        'post_type' => 'post',
        'showposts' => 3,
        'post__not_in' => array( $post->ID ),
        'post_status' => array('publish'),
        'orderby' => 'date',
        'order' => 'DESC',
        'tax_query' => array(
            array(
                'taxonomy' => 'post_tag',
                'field' => 'slug',
                'terms' => array('interior-systems-related') )
            )
        );

if ( has_term( 'interior-systems', 'categories' ) ) {
    $args['tax_query']['terms'] = array('interior-systems-related');
} else if ( has_term( 'solar-signs-streetscape-elements', 'categories' ) ) {
    $args['tax_query']['terms'] = array('solar-signs-related');
}

答案 1 :(得分:0)

你可以创建一个数组术语,它包含所需的类别,而不是形成args:

  <?php
  $terms = array();
  if ( has_term( 'interior-systems', 'categories' ) ) {
    $terms[] = 'interior-systems';

  }
  if ( has_term( 'solar-signs-streetscape-elements', 'categories' ) ) {
    $terms[] = 'solar-signs-streetscape-elements';
  }
  if (!empty($terms)) {
    $args = array(
      'post_type' => 'post',
      'showposts' => 3,
      'post__not_in' => array( $post->ID ),
      'post_status' => array('publish'),
      'orderby' => 'date',
      'order' => 'DESC',
        'tax_query' => array(
         array(
          'taxonomy' => 'post_tag',
          'field' => 'slug',
          'terms' => $terms )
       )
     );
    );
  }
  $query = new WP_Query($args);

  if ($query->have_posts()) : while ($query->have_posts()) :       
  $query->the_post(); ?>           
  <div class="post-wrap relat relatblog">
  <h2 class="entry-title"><?php the_title(); ?></h2>
  </div>                   
    <?php endwhile;
    wp_reset_postdata();
    endif;
    endwhile; ?>

答案 2 :(得分:0)

试试这个。

if ( has_term( array('interior-systems', 'solar-signs-streetscape-elements'), 'categories' ) ) {
  $args = array(
  'post_type' => 'post',
  'showposts' => 3,
  'post__not_in' => array( $post->ID ),
  'post_status' => array('publish'),
  'orderby' => 'date',
  'order' => 'DESC',
    'tax_query' => array(
     array(
    'taxonomy' => 'post_tag',
    'field' => 'slug',
    'terms' => array('interior-systems-related', 'solar-signs-related') )
    )
);
}

$query = new WP_Query($args);

if ($query->have_posts()) : while ($query->have_posts()) :       
$query->the_post(); ?>           
<div class="post-wrap relat relatblog">
<h2 class="entry-title"><?php the_title(); ?></h2>
</div>                   
  <?php endwhile;
  wp_reset_postdata();
  endif;
  endwhile; ?>

https://codex.wordpress.org/Function_Reference/has_term https://codex.wordpress.org/Class_Reference/WP_Query