【发布时间】:2017-10-16 20:29:36
【问题描述】:
我正在尝试从要加载到我的 WooCommerce 网站的 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 = "Not Available";
$vendorName = the_field('apmex_vendor_name');
// loop the json array
foreach ($json['coin'] as $value){
// check the condition
if ($value['name'] == $vendorName){
$coinPrice = $value['price']; // get the price
break; // exit the loop
}
}
echo $coinPrice;
【问题讨论】:
-
这里有什么问题.... the_field('apmex_vendor_name') 不起作用吗?因为 foreach 在我的测试中运行良好
-
所以结果是“apmex_vendor_name”的值打印在“不可用”旁边,而不是返回与“apmex_vendor_name”字段匹配的对象名称的结果。我说得通吗?
标签: php json wordpress woocommerce advanced-custom-fields