【问题标题】:Conditional queries WP_query based on term基于术语的条件查询 WP_query
【发布时间】:2017-09-07 08:04:25
【问题描述】:

基本上想用 term = 艺术显示第一个帖子,如果不存在则显示带有 term = guides 的帖子。

这是我的两个论点,我一直在尝试在循环外使用 has_term 但不起作用。

<?php

if ( has_term( 'arts', 'postkicker' )){

$args=array(
    'post_type' => 'post',
    'taxonomy'  => 'postkicker',
    'term'      => 'arts',
    'orderby'   => 'date',
    'order'     => 'DESC'
);

} else {

$args=array(
    'post_type' => 'post',
    'taxonomy'  => 'postkicker',
    'term'      => 'guides',
    'orderby'    => 'date',
    'order'      => 'DESC'
);

} 

$my_query = new WP_Query($args);

if ( $my_query->have_posts() ) :
    while ( $my_query->have_posts() ) : $my_query->the_post();
    the_title( sprintf( "<h1><a href='%s' rel='bookmark'>", esc_url( get_permalink() ) ), "</a></h1>" ); 

    endwhile;wp_reset_postdata();endif; 
?>

有什么建议吗?我需要在循环中移动我的 has_term 吗? 提前致谢!

【问题讨论】:

    标签: php wordpress conditional


    【解决方案1】:

    你很接近 -> 但你应该尝试

    <?php
    
    $first_args=array(
        'post_type' => 'post',
        'taxonomy'  => 'postkicker',
        'term'      => 'arts',
        'orderby'   => 'date',
        'order'     => 'DESC'
    );
    
    
    $second_args=array(
        'post_type' => 'post',
        'taxonomy'  => 'postkicker',
        'term'      => 'guides',
        'orderby'    => 'date',
        'order'      => 'DESC'
    );
    
    $first_query = new WP_Query($first_args);
    
    if ($first_query->have_posts() ) {
         // Do your HTML
     }
     else {
     $second_query = new WP_Query($second_args);
     if ($second_query->have_posts() ){
        // Do your HTML
        }     
     }
    

    这应该足以让你开始:)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-12
      • 1970-01-01
      • 1970-01-01
      • 2016-10-16
      • 2014-08-04
      相关资源
      最近更新 更多