【发布时间】: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