php – WP REST API从帖子类型中获取帖子

前端之家收集整理的这篇文章主要介绍了php – WP REST API从帖子类型中获取帖子前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
如何使用WP REST API(v1或v2)获取特定自定义帖子类型的所有帖子?我对此非常陌生,并试图了解如何做到这一点.

我目前正在使用WP REST API v2并设法使用此获取所有帖子类型的列表

http://domain.com/wp-json/wp/v2/types

然后设法得到我感兴趣的帖子类型

http://domain.com/wp-json/wp/v2/types/the-icons-update

如何从特定内容类型中获取所有帖子?

我试过了

http://domain.com/wp-json/wp/v2/posts?filter[post_type]=the-icons-update

但它返回一个空数组(我想它会返回默认帖子,而在我的网站上只有我试图检索的自定义帖子类型中的帖子).

我如何注册帖子类型会有问题吗?

function custom_post_type() {
$labels = array(
    'name'               => _x( 'The Icons Update','post type general name' ),'singular_name'      => _x( 'The Icons Update','post type singular name' ),'add_new'            => _x( 'Add Page','magazine' ),'add_new_item'       => __( 'Add New Page' ),'edit_item'          => __( 'Edit Page' ),'new_item'           => __( 'New Page' ),'all_items'          => __( 'All Pages' ),'view_item'          => __( 'View Page' ),'search_items'       => __( 'Search Pages' ),'not_found'          => __( 'No Page found' ),'not_found_in_trash' => __( 'No Page found in the Trash' ),'parent_item_colon'  => '','menu_icon'          => '','menu_name'          => 'The Icons Update'
);
$args = array(
    'labels'        => $labels,'description'   => 'Holds our projects and project specific data','public'        => true,'menu_position' => 5,'supports'      => array( 'title','editor','thumbnail','excerpt','custom-fields' ),'has_archive'   => true,'taxonomies'    => array('post_tag','category'),'hierarchical'  => false,'query_var'     => true,'queryable' => true,'searchable'    => true,'rewrite'       => array( 'slug' => 'the-icons-update' )
);
register_post_type( 'magazine',$args );
flush_rewrite_rules();
}
add_action( 'init','custom_post_type' );

任何帮助都非常感谢.

对于v.2,有一个非常简单的方法.您所要做的就是在args数组中包含以下属性:’show_in_rest’=>真正

例:

register_post_type( 'recipe',array(
          'labels' => $labels,'public' => true,'hierarchical' => false,'supports' => $supports,'show_in_rest' => true,'taxonomies' => array('recipe-type','post_tag'),'rewrite' => array( 'slug' => __('recipe','recipe') )
     )
);

猜你在找的PHP相关文章