在我的自定义函数插件中
function dropin_location() {
if (is_page ('7')) {
echo '<script>';
echo "alert('got here')";
echo '</script>';
}
}
add_action('init', 'dropin_location');在保存时,我得到403错误:“拒绝访问服务器上的该资源!”请问我做错什么了?
发布于 2019-11-30 21:40:11
您可以使用wp_head。这将把脚本插入头部。
function dropin_location() {
if ( is_page( 7 ) ) {
?>
<script>alert('got here')</script>'
<?php
}
}
add_action('wp_head', 'dropin_location');但是,还有其他添加脚本的方法。您可以将它作为脚本文件保存在主题中,并将其放入队列,以便WordPress“了解”它。这样,它就可以有依赖项,在正确的时间加载,等等这里有一个解释。
因此,如果主题中有my-killer-script.js,则可以执行以下操作:
wp_enqueue_script(
'some-script-handle', // some name to give it
get_theme_file_uri( '/path/to/my-killer-script.js' ), // file path to script
array( 'some-dependency-handle' ), // does this script need any dependencies? If so, add their handles, or leave the array empty.
false, // do you need a version?
true // load in footer? true/false
);https://wordpress.stackexchange.com/questions/353656
复制相似问题