基于术语的条件查询WP_query

时间:2017-04-11 19:08:03

标签: php wordpress conditional

基本上想要显示带有term = arts的第一篇帖子,如果不存在,则显示带有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? 提前谢谢!

1 个答案:

答案 0 :(得分:1)

你很接近 - &gt;但你应该尝试

application.properties

这应该足以让你开始:)

相关问题