程序问答   发布时间:2022-06-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了如何在按类别 (Wordpress) 获取帖子时使用 _embed 参数?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决如何在按类别 (Wordpress) 获取帖子时使用 _embed 参数??

开发过程中遇到如何在按类别 (Wordpress) 获取帖子时使用 _embed 参数?的问题如何解决?下面主要结合日常开发的经验,给出你关于如何在按类别 (Wordpress) 获取帖子时使用 _embed 参数?的解决方法建议,希望对你解决如何在按类别 (Wordpress) 获取帖子时使用 _embed 参数?有所启发或帮助;

大家好,感谢您的时间。我 @H_696_3@fetching all the posts 使用 @H_696_3@categorIEs= 参数如下:

@H_696_3@https://recipes.turboweb.online/wp-Json/wp/v2/posts?categorIEs=2

此处类别的 ID 为 @H_696_3@2。因此,它为我获取了包含 categoryID 2 的所有帖子。但是,此请求不会获取 @H_696_3@Comments 和 @H_696_3@Authorname 以及更多必需的数据。

我知道通过对普通查询使用 @H_696_3@_embed,我们将在单个查询中获取所有数据,从而节省时间和资源。这是查询的样子:

@H_696_3@https://recipes.turboweb.online/wp-Json/wp/v2/posts?_embed

这是输出:

如何在按类别 (Wordpress) 获取帖子时使用 _embed 参数?

我的问题是如何在参数中使用 @H_696_3@_embed 来获取特定 categoryID 的帖子?用更简单的术语来说,我如何下面的查询中插入 @H_696_3@_embed 参数?

@H_696_3@https://recipes.turboweb.online/wp-Json/wp/v2/posts?categorIEs=2

解决方法

在我的本地测试中,这个请求工作正常:

http://localhost/wp-json/wp/v2/posts?categories=104&_embed

print of the return test

要返回评论,您需要在 rest_api_init 钩子中应用一个函数,如下所示:

@H_696_3@add_action ( 'rest_api_init','add_custom_fields_to_api' ) ;

function add_custom_fields_to_api () {
    register_rest_field ( 'post','comments',[
            'get_callBACk' => 'cb_get_comments'
        ]
    );

}

function cb_get_comments( $object ) {
    // get your comments,for example with the function get_comments()
}

大佬总结

以上是大佬教程为你收集整理的如何在按类别 (Wordpress) 获取帖子时使用 _embed 参数?全部内容,希望文章能够帮你解决如何在按类别 (Wordpress) 获取帖子时使用 _embed 参数?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

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