获取与高级自定义字段匹配的json数据

时间:2017-10-16 16:58:36

标签: php json wordpress advanced-custom-fields

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

$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;

1 个答案:

答案 0 :(得分:1)

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

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

如果您可以使用JSON the_field值并输入公共网址。

一旦我们拥有这两个数据集,就应该很容易找出为什么$ value name不等于product_attribute。