最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

functions - How to add javascript code into Divi child theme?

programmeradmin0浏览0评论

I want to add this JavaScript code into my Divi child theme. I have made a directory for the js file inside the Divi child theme folder. The name of the js file is "selectpage.js".

<script type="text/javascript">
jQuery(document).ready(function(){
jQuery(".select_page").text("your-text-here");
});
</script>

I added this code into my functions.php file. This is not working, I think I am doing something wrong with the wp_enqueue_script directory. Because when I go to inspect on the site it tries to locate the js files in the /wp-incldudes/js/scripts. Does it matter where I put the js file inside my child theme folder? It is only for changing the text in the mobile center-menu "select_page".

<?php
function my_theme_enqueue_styles() {

    $parent_style = 'divi-style';

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


**wp_enqueue_script( 'et_mobile_nav_menu', $template_dir . '/js/selectpage.js', array( 'jquery' ), true );**



?>

Thanks in Advance, Davíð

I want to add this JavaScript code into my Divi child theme. I have made a directory for the js file inside the Divi child theme folder. The name of the js file is "selectpage.js".

<script type="text/javascript">
jQuery(document).ready(function(){
jQuery(".select_page").text("your-text-here");
});
</script>

I added this code into my functions.php file. This is not working, I think I am doing something wrong with the wp_enqueue_script directory. Because when I go to inspect on the site it tries to locate the js files in the /wp-incldudes/js/scripts. Does it matter where I put the js file inside my child theme folder? It is only for changing the text in the mobile center-menu "select_page".

<?php
function my_theme_enqueue_styles() {

    $parent_style = 'divi-style';

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


**wp_enqueue_script( 'et_mobile_nav_menu', $template_dir . '/js/selectpage.js', array( 'jquery' ), true );**



?>

Thanks in Advance, Davíð

Share Improve this question edited Sep 23, 2019 at 11:55 Gufran Hasan 6918 silver badges20 bronze badges asked Sep 23, 2019 at 8:31 davidhlynsdavidhlyns 178 bronze badges 1
  • 1 Scripts should be enqueued using the wp_enqueue_scripts hook - example - and take time to thoroughly read the wp_enqueue_script() documentation. For Divi-specific stuff (like Divi script handle), consult the Divi support forums/site. – Sally CJ Commented Sep 23, 2019 at 9:53
Add a comment  | 

1 Answer 1

Reset to default 0

You're almost there, change code in functions.php to:

<?php
function my_theme_enqueue_styles() {

    $parent_style = 'divi-style';

    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')
    );
    wp_enqueue_script( 'et_mobile_nav_menu', get_stylesheet_directory_uri() . '/js/selectpage.js', array( 'jquery' ), true );
}
add_action( 'wp_enqueue_scripts', 'my_theme_enqueue_styles' );
?>

Now your script should be located at: /wp-content/themes/divi-child/js/selectpage.js

发布评论

评论列表(0)

  1. 暂无评论