wordpress 更改 post api

wordpress make changes to post api

我们在博客上使用 wordpress(通过 Godaddy 托管)。我不是 wordpress 开发人员,因此不是这个基本问题。我们有像 blog.domainname.com/wp-json/wp/v2/posts 这样的 wordpress post api。这个returns一个json对象的大部分字段。现在我添加了一个自定义字段,将与此对象一起返回。我看过很多视频并研究了需要做哪些改变,尤其是这里的那个。 https://developer.wordpress.org/rest-api/extending-the-rest-api/modifying-responses/

我的问题是 - 我在哪里进行这些更改?我需要什么文件来添加这些更改?我以管理员身份使用 WP。我安装了文件资源管理器插件,我可以看到三个主要文件夹 - wp-admin、wp-content、wp-includes。

是否有我需要更改的特定文件?根据我的学习 - 我写了以下片段。不确定在哪里添加这个:(

function addCustomField_register_fields() {

register_rest_field('post',
        'custom_Field_name',
        array(
            'get_callback'      =>'get_custom_field',
            'update_callback'   => null,
            'schema'        => null
        ));

    }

function get_custom_field($object, $field_name , $request) {
    return get_post('custom_field');
    }

add_action('rest_api_init', 'addCustomField_register_fields');

如果您能告诉我我的方法是否正确,也将不胜感激。

通常,主题定制会添加到活动主题根目录中的 functions.php 文件中。

至于你的代码,你想做什么?特别是您的回调看起来不会起作用。 get_post() 按 ID 加载 WordPress post。 'custom_field' 不是有效的 post ID,因此这总是会失败。