简体   繁体   中英

Why does my functions.php keep crashing (wordpress)? - new to Javascript

I am using wordpress. Within functions.php, I am trying to add the following piece of code

function load_js_assets() {
    if( is_page( A53 ) ) {
        wp_enqueue_script('a53wx2.js', '', array('jquery'), '', false);
    } 
}

add_action('wp_enqueue_scripts', 'load_js_assets');

When I add this, it is fine. However, when I try and add another function, similar to the one above, it crashes the site. This is the code which crashes the site:

<?php
}
add_filter( 'get_comments_number', 'blankslate_comment_count', 0 );
function blankslate_comment_count( $count ) {
if ( ! is_admin() ) {
global $id;
$get_comments = get_comments( 'status=approve&post_id=' . $id );
$comments_by_type = separate_comments( $get_comments );
return count( $comments_by_type['comment'] );
} else {
return $count;
}
}
function load_js_assets() {
    if( is_page( A53 ) ) {
        wp_enqueue_script('a53wx2.js', '', array('jquery'), '', false);
    } 
}

add_action('wp_enqueue_scripts', 'load_js_assets');
}
function load_js_assets() {
    if( is_page( A5004 ) ) {
        wp_enqueue_script('a5004wx2.js', '', array('jquery'), '', false);
    } 
}

add_action('wp_enqueue_scripts', 'load_js_assets');

Which is the correct way to add the second function so that it does not crash the site? Thank you

Your function name should be unique this will throw cann't redeclare function error so you need to change your function name

 function load_js_assets() {
    if( is_page( A53 ) ) {
        wp_enqueue_script('a53wx2.js', 'http://peakweathereye.co.uk/a53wx2.js', array('jquery'), '', false);
    } 
}

add_action('wp_enqueue_scripts', 'load_js_assets');

function load_js_assets1() {
    if( is_page( A5004 ) ) {
        wp_enqueue_script('a5004wx2.js', 'http://peakweathereye.co.uk/a5004wx2.js', array('jquery'), '', false);
    } 
}

add_action('wp_enqueue_scripts', 'load_js_assets1');

Your are loading the same function 'load_js_assets' twice.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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