Javascript 无法在子主题中添加自定义js脚本
我需要在wordpress的子主题中添加自定义脚本 这是我的孩子主题的内容:Javascript 无法在子主题中添加自定义js脚本,javascript,wordpress,Javascript,Wordpress,我需要在wordpress的子主题中添加自定义脚本 这是我的孩子主题的内容: themes |meteorite |style.css |meteorite-child |function.php |style.css |js |mngclk.js my function.php add_action( 'wp_enqueue_scripts', 'theme_enqueue_styles' ); function theme_enq
themes
|meteorite
|style.css
|meteorite-child
|function.php
|style.css
|js
|mngclk.js
my function.php
add_action( 'wp_enqueue_scripts', 'theme_enqueue_styles' );
function theme_enqueue_styles()
{
wp_enqueue_style('parent-style', get_template_directory_uri() . '/style.css');
}
wp_register_script( 'new-script', get_stylesheet_directory_uri() . '/js/mngclk.js', 'jquery', "1", true);
wp_enqueue_script( 'new-script' );
function add_child_theme_scripts() {
wp_enqueue_style( 'parent-style', get_template_directory_uri() . '/style.css', array(), '1.0' );
/* wp_enqueue_script( 'new-script', get_stylesheet_directory_uri() . '/js/mngclk.js', array('jquery'), '1.0', true ); */
}
add_action( 'wp_enqueue_scripts', 'add_child_theme_scripts' );
我尝试了多次更改,但没有成功地包含我的自定义js脚本。这是一个正确的示例
function my_theme_enqueue_styles() {
$parent_style = 'parent-style'; // This is 'twentyfifteen-style' for the Twenty Fifteen theme.
wp_enqueue_style( $parent_style, get_template_directory_uri() . '/style.css' );
wp_enqueue_style( 'child-style',
get_stylesheet_directory_uri() . '/style.css',
array( $parent_style ),
wp_get_theme()->get('Version')
);
}
add_action( 'wp_enqueue_scripts', 'my_theme_enqueue_styles' );
请点击此链接了解更多信息
在child-theme function.php中添加以下函数
add_action( 'wp_enqueue_scripts', 'theme_enqueue_styles' );
function theme_enqueue_styles()
{
wp_enqueue_style('parent-style', get_template_directory_uri() . '/style.css');
}
wp_register_script( 'new-script', get_stylesheet_directory_uri() . '/js/mngclk.js', 'jquery', "1", true);
wp_enqueue_script( 'new-script' );
function add_child_theme_scripts() {
wp_enqueue_style( 'parent-style', get_template_directory_uri() . '/style.css', array(), '1.0' );
/* wp_enqueue_script( 'new-script', get_stylesheet_directory_uri() . '/js/mngclk.js', array('jquery'), '1.0', true ); */
}
add_action( 'wp_enqueue_scripts', 'add_child_theme_scripts' );
在子主题function.php文件中添加以下函数
<?php
function myscript() {
?>
<script type="text/javascript">
function mngClk(str){
document.location.href= decodeURIComponent(escape(window.atob(str)));
}
</script>
<?php
}
add_action( 'wp_footer', 'myscript' );
?>
功能mngClk(str){
document.location.href=decodeURIComponent(escape(window.atob(str));
}
Style.css没问题,我的问题是js文件。我已经读过这个链接,但是没有关于javascripti的信息。我想wp_register_script函数的第三个参数是,你只使用了“jquery”,你应该使用像这个数组(“jquery”)这样的数组。我试过了,但我仍然无法查看加载在头部的js文件。当我尝试调用js中的函数时。它仍然说uncaughtreferenceerror:mngClk没有定义在HTMLSpanElement.onclick((索引):318)请向我们提供网站链接和子主题文件路径。因为您的js位于子主题js文件夹中。因此,将js放在子主题/js/mngclk.js中该网站是[link]tuyauarrosage.fr,我试图在function.php中直接设置:但这并不能解决问题,尽管我可以使用web浏览器正确访问该文件。同样的问题。(你提供的解决方案实际上是上传的)我猜问题是否不是来自elsewere…我复制粘贴了你给我的全部代码。这两个模块,第一个关于样式css,第二个关于javascript。问题仍然存在。我会在没有任何插件的其他wordpress上检查它,以检测是否缺少某些东西。