我在短代码中使用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' );