You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在WordPress中用WPGetAPI短代码仅显示JSON API单个值?

解决WPGetAPI免费版提取单个JSON值的问题

核心问题原因

你当前将端点的Result Format设置为JSON String,而WPGetAPI免费版的keys参数仅在结果格式为Array/Object时才会生效——JSON字符串模式下插件不会解析数据结构,自然无法识别并提取指定字段。

直接解决步骤

  1. 修改端点设置

    • 进入WPGetAPI的端点配置页面(对应trail_1端点)
    • 将Result Format从JSON String改为Array/Object
    • 保存设置
  2. 使用原有短代码
    保持你之前的短代码不变:

    [wpgetapi_endpoint api_id="trail" endpoint_id="trail_1" keys="data.total_auctions"]
    

    此时插件会自动解析API返回的JSON结构,提取data.total_auctions对应的数值103并输出。

备选方案(无法修改结果格式时)

如果因特殊需求不能调整Result Format,可以通过自定义短代码实现:

  1. 在主题的functions.php文件中添加以下代码:
    function get_total_auctions() {
        // 获取WPGetAPI返回的原始JSON字符串
        $json_str = do_shortcode('[wpgetapi_endpoint api_id="trail" endpoint_id="trail_1"]');
        // 解析JSON为关联数组
        $response_data = json_decode($json_str, true);
        // 返回目标值,同时处理数据异常情况
        return isset($response_data['data']['total_auctions']) ? $response_data['data']['total_auctions'] : '0';
    }
    // 注册自定义短代码
    add_shortcode('total_auctions', 'get_total_auctions');
    
  2. 在Elementor中使用自定义短代码[total_auctions]即可显示目标数值。

内容的提问来源于stack exchange,提问作者Leaner69

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.12 10:52:02