**上下文:**我正在开发一个自定义的WordPress主题。
**目标:**在主页上,当你点击时,“stamps”会在窗口中出现。我已经运行了js。下面是一个例子,你可以清楚地看到我在做什么:https://omg-draft.superhi.com/
**问题:**我需要一种方法来访问我的main.js中的veggies。我在将它们从functions.php传递到main.js时遇到了问题。我已经将js文件排队并传递了图像文件。在控制台中,我返回了“veggies is not defined”。
下面是functions.php中的代码
add_action( 'admin_init', 'omedte_get_scripts' );
function omedte_get_scripts() {
wp_enqueue_script('main.js', get_template_directory_uri() . "/main.js", array(), null, true);
$veggies = array(
"beetroot" => get_theme_file_uri('/assets/beetroot.svg'),
"broccoli" => get_theme_file_uri('/assets/broccoli.svg'),
"capsicum" => get_theme_file_uri('/assets/capsicum.svg'),
"carrot" => get_theme_file_uri('/assets/carrot.svg'),
"corn" => get_theme_file_uri('/assets/corn.svg'),
"gourd" => get_theme_file_uri('/assets/gourd.svg'),
"kale" => get_theme_file_uri('/assets/kale.svg'),
"pumpkin" => get_theme_file_uri('/assets/pumpkin.svg'),
"spinach" => get_theme_file_uri('/assets/spinach.svg')
);
wp_localize_script( 'omedte_javascript_file', 'veggies', $veggies );
}
这是我主要.js:
let carrot = veggies.carrot;
let broccoli = veggies.broccoli;
let capsicum = veggies.capsicum;
let stamps = [
carrot,
broccoli,
capsicum
]
1条答案
按热度按时间ee7vknir1#
这可能不是唯一的问题,但您的句柄在enqueue和localise语句中不匹配。
请参阅wp_localize_script()