Php 如何使用post_type、posts_per_page和id获取自定义类型的posts筛选
在我的快捷代码中,我想从我的自定义文章中获取文章,但要使用ID、文章类型和每页文章过滤它们 e、 g.Php 如何使用post_type、posts_per_page和id获取自定义类型的posts筛选,php,wordpress,custom-post-type,Php,Wordpress,Custom Post Type,在我的快捷代码中,我想从我的自定义文章中获取文章,但要使用ID、文章类型和每页文章过滤它们 e、 g.[myshortcode type=“my_custom_type_post”]-显示my_custom_type_post中的所有帖子 或[myshortcode type=“my_custom_type_post”no_of_posts=“3”]-仅显示3篇文章 或[myshortcode type=“my_custom_type_post”no_posts=“1”id=“112”]-显示特
[myshortcode type=“my_custom_type_post”]
-显示my_custom_type_post中的所有帖子
或[myshortcode type=“my_custom_type_post”no_of_posts=“3”]
-仅显示3篇文章
或[myshortcode type=“my_custom_type_post”no_posts=“1”id=“112”]
-显示特定的帖子
这是我的密码。当我只发送每个页面的类型和帖子以及ID时,它可以工作,但当我想显示所有帖子或者没有帖子时,它就不工作了
// 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));
}
我只需要根据条件设置参数只需使用
'post\u in'=>$id\u数组
,因为当我想显示所有帖子或没有postscheck我的答案时,你已经将数组传递给了values仍然不起作用