在我的快捷代码中,我想从我的自定义文章中获取文章,但要使用ID、文章类型和每页文章过滤它们
例如[my短码type="my_custom_type_post"]
-显示my_custom_type_post中的所有帖子
或[myshortcode type=“my_custom_type_post”no_of_posts=“3”]
-仅显示3篇文章
或者[my短码type="my_custom_type_post"no_of_posts="1"id="112"]
-显示特定的帖子
这是我的代码。它工作时,我只发送类型和posts_per_page和id但不工作时,我想显示所有的职位或与no_of_posts
// extracting values from shortcode
extract( shortcode_atts( array (
'type' => '',
'no_of_posts' => '',
'id' => ''
), $atts ) );
$id_array = array_map('intval',explode(',',$id));
$args = array(
'post_type' => $type,
'posts_per_page' => $no_of_posts,
'post__in' => $id_array
);
然后将这些值传递给wp_查询
$query = new WP_Query( $args );
if( $query->have_posts() ){
/*printing out results*/
}
您必须使用以下条件代码
extract( shortcode_atts( array (
'type' => '',
'no_of_posts' => '',
'id' => ''
), $atts ) );
$args['post_type'] = $type;
if($no_of_posts) {
$args['posts_per_page'] = $no_of_posts;
} else {
$args['posts_per_page'] = '-1';
}
if($id) {
$args['post__in'] = array_map('intval',explode(',',$id));
}
我只是根据条件设置参数