如何将这两种"if"条件结合起来?



使用此代码,我在自定义帖子"产品"页面上获得了包含"太阳标志相关"标签的最后 3 个帖子,这些标签具有"太阳标志-街景元素"术语。

但是,如果自定义帖子"产品"页面是"内部系统"

,如何组合两个"如果",以便它也将显示具有"内部系统相关"标签的博客文章?

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; ?>

你可以尝试这样的事情:

$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');
}

您可以创建一个数组术语,其中包含所需的类别,然后用它们形成参数:

  <?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; ?>

试试这个。

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_termhttps://codex.wordpress.org/Class_Reference/WP_Query

最新更新