13 次查询 耗时 0.054 秒
本文共计549个字,0条留言,预计阅读时长2分钟
登录/注册
  • 首页
  • >
  • Notes
  • >
  • 如何向WordPress rest API中添加自定义内容
  • 如何向WordPress rest API中添加自定义内容

    作者:Misshylsay

    日期:2020年08月18日

    今天帮朋友做一个有关微信小程序的Wordpress插件,其中需要向Wordpress rest api中加入一些自定义内容,基于开源分享给大家:

    add_action( 'rest_api_init', 'my_rest_get_post_id' ); //注册函数加入 REST API
    
    function my_rest_get_post_id(){ //注册函数
        register_rest_field( 'post', 
            'post_lunbo_ids',  //要注册的字段名
            array(
                'get_callback' => 'get_post_ids', //注册的要实现的功能函数
                'update_callback' => null,
                'schema' => null,
            )
        );
    }
    
    function get_post_ids($post){ //要实现的功能函数
        $fengrui_options = get_option( 'fengrui_option_name' ); // Array of All Options
        $api_key_0 = $fengrui_options['api_key_0']; // API Key
        $id_1 = $fengrui_options['id_1']; // 文章轮播ID
        return $id_1;
    }

    将以上代码直接复制粘贴到插件即可。

    官方参考文档

    “如何向WordPress rest API中添加自定义内容”共有0条评论

    发表评论

    B em del U Link Code Quote