【问题标题】:Get json data that matches advanced custom field获取与高级自定义字段匹配的 json 数据
【发布时间】:2017-10-16 16:58:36
【问题描述】:

我正在尝试从要加载到我的 wordpress 网站的 json 文件中获取数据。我想从我抓取的网站产品的匹配名称中获取价格。我需要产品的名称来匹配我添加到我添加到 wordpress 产品页面的产品页面的值高级自定义字段,然后如果名称与我添加的属性匹配则获取价格。下面的代码部分工作,但由于某种原因,高级自定义字段的调用不起作用。它显示文本的值,而不是将其与 json 中的 name 字段匹配有什么建议吗?

$str = file_get_contents('http://gold.explorethatstore.com/wp-content/themes/divi-ETS-child-theme/run_results_apmex.json');



                // decode JSON
                $json = json_decode($str, true);

                // default value
                $coinPrice = "No results found";
                $product_attribute = the_field('apmex_vendor_name');
                // loop the json array
                foreach($json['coin'] as $value){
                    // check the condition
                    if($value['name'] == $product_attribute){
                        $coinPrice = $value['price']; // get the price
                        break; // exit the loop
                    }
                }

                echo $coinPrice;

【问题讨论】:

  • the_field() ACF 函数也存在同样的问题:使用 get_field() 代替 $product_attribute = get_field('apmex_vendor_name');...

标签: php json wordpress advanced-custom-fields


【解决方案1】:

您提供的 JSON Url 未返回 JSON,目前正在返回 404。

另外,我们看不到 product_attribute 的值,因为它是从数据库返回的。

如果您可以 JSON the_field 值并放入公共 url。

一旦我们有了这两个数据集,应该很容易弄清楚为什么 $value 名称不等于 product_attribute。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-07-07
    • 2018-03-28
    • 2018-05-01
    • 2014-02-06
    • 1970-01-01
    • 2016-05-31
    • 2017-01-06
    相关资源
    最近更新 更多