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

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了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 – WP REST API从帖子类型中获取帖子全部内容,希望文章能够帮你解决php – WP REST API从帖子类型中获取帖子所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。