【问题标题】:Add fields in Json Response of Posts REST API在帖子 REST API 的 Json 响应中添加字段
【发布时间】:2017-06-20 06:57:47
【问题描述】:

我想在帖子的回复中添加一个自定义字段,但是当我在一段时间后添加新字段时,它会从文件中删除。

我修改它的 class-wp-rest-posts-controller.php 文件并在 prepare_item_for_response 函数中添加新字段

我添加了这行,效果很好,但是用了一段时间就被删除了

foreach ( $taxonomies as $taxonomy ) {
            $base = "other";
            $terms = get_the_terms( $post, $taxonomy->name );
            $datres = $terms ? array_values( wp_list_pluck( $terms, 'name' ) ) : array();
            $data[ $base ] = implode(" ", $datres);
        }

知道为什么会这样吗?

【问题讨论】:

    标签: php json wordpress rest wordpress-rest-api


    【解决方案1】:

    在 WordPress 中,您不应编辑核心文件或核心插件,因为文件已更新,您的更改将被删除。正确的方法是通过子主题/您自己的插件中的 Hooks 和函数。

    WP Child theme

    WP Rest api custom end point docs

    【讨论】:

    • 哦,真的吗?我想这会发生……好吧,我尝试添加自定义端点。谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-24
    • 2018-11-30
    • 2021-01-01
    • 2019-02-21
    • 2015-12-08
    • 1970-01-01
    相关资源
    最近更新 更多