带有两组参数的Wordpress get_posts

时间:2019-05-10 13:29:36

标签: php wordpress

我在短代码中使用get_posts()在分类页面上获取自定义帖子。如果不在分类法页面上,我想使用相同的脚本提取所有帖子。我知道我需要对get_posts()使用不同的参数集,但不确定这样做的最干净方法吗?

function output_inspiration() {

  $category = get_queried_object()->term_id;

  $args = array(
    'post_type' => 'inspiration',
    'posts_per_page' => 5,
    'tax_query' => array(
            array(
                'taxonomy' => 'inspiration_category',
                'field' => 'term_taxonomy_id',
                'terms' => $category
            )
         )
  );

  $posts = get_posts($args);

//do something with posts
}
add_shortcode( 'inspiration', 'output_inspiration' );

0 个答案:

没有答案