假设您使用wp_enqueue_scripts
并为其分配了一个适当的句柄,此功能将允许您访问postage_prices
通过变量在jQuery中输入字段postagePricesArray.prices
. 您需要更换%YOUR_SCRIPT_HANDLE%
使用jQuery脚本的实际句柄,否则此变量可能无法访问
function localize_postage_prices() {
global $post;
$id = $post->ID;
if(have_rows(\'postage_prices\', $id)) {
$postage_prices_array = array();
foreach(get_field(\'postage_prices\', $id) as $price) {
$postage_prices_array[] = array(
\'order_qty\' => $price[\'order_qty\'],
\'strips_rp\' => $price[\'strips_rp\'],
\'strips_ep\' => $price[\'strips_ep\'],
\'roll_rp\' => $price[\'roll_rp\'],
\'roll_ep\' => $price[\'roll_ep\']
);
}
//
// You need to replace %YOUR_SCRIPT_HANDLE% with your script\'s actual handle!
//
wp_localize_script(\'%YOUR_SCRIPT_HANDLE%\', \'postagePricesArray\', array(
\'prices\' => $postage_prices_array
));
}
}
add_action(\'wp_enqueue_scripts\', \'localize_postage_prices\');
然后在jQuery文件中(该文件已排队,句柄为
%YOUR_SCRIPT_HANDLE%
)您可以访问阵列:
$(function() {
console.log(postagePricesArray.prices);
});