繁体   English   中英

无法在子主题中添加自定义js脚本

[英]unable to add custom js script in child theme

我需要在wordpress的子主题中添加自定义脚本。

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

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

我尝试了多次更改,但没有设法包含自定义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' );

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

在此处输入链接说明

在子主题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' );
?>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM