2017-07-25 74 views
0

我需要在WordPress中的我的子主题中添加自定义脚本。无法在儿童主题中添加自定义js脚本

这是我的孩子主题的内容:

themes  
    |meteorite 
    |style.css 
    |meteorite-child 
    |function.php 
    |style.css 
    |js 
     |mngclk.js 

我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'); 

我试过多次变化,但我没能inclute我自定义的js脚本

回答

0

这是适当的例子

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'); 

请点击此链接获取更多信息

enter link description here

+0

风格.css是好的,我的问题是与js文件。 我已经阅读了这个链接,但没有javascript的信息 –

+0

我认为wp_register_script函数的第三个参数,你只使用'jquery',你应该使用这个数组('jquery') –

0

儿童主题function.php下面添加功能

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'); 
?> 
+0

我试过但我仍然无法查看加载在头部的js文件。当我尝试调用js中的函数时。它仍然说Uncaught ReferenceError:mngClk未定义在HTMLSpanElement.onclick((index):318) –

+0

请为我们提供网站链接和子主题文件路径。因为你的js在儿童主题js文件夹中。所以把js放在儿童主题/ js/mngclk.js –

+0

该网站是[链接] tuyauarrosage.fr 我试图设置:https://tuyauarrosage.fr/wp-content/themes/meteorite-child/js/mngclk .js直接在function.php中,但是这并不能解决问题,尽管我可以使用Web浏览器正确访问文件。 –

相关问题