admin管理员组

文章数量:1287575

I am battling with a problem for months now, that my code will not execute or show the result, when I enqueue it. I have been told, that my code is fine, and that it should work. The clock is showing, so the css and html is correct, but not doing anything. Here it is:

<?php

function childtheme_parent_styles() {
 wp_enqueue_style( 'parent', get_template_directory_uri().'css/style.css' );
function mytheme_files() {
 wp_enqueue_style('mytheme_main_style', get_stylesheet_uri());
wp_register_script('main-js', get_stylesheet_directory_url() . '/js/main.js');
}

add_action( 'wp_enqueue_scripts', 'childtheme_parent_styles');

wp_register_script('main-js',get_stylesheet_directory_url().'/js/main.js');

}

add_action( 'wp_enqueue_scripts', 'pr_scripts_styles' );
}

function pr_scripts_styles() {
if( is_page(507) ) {
wp_enqueue_script( 'main-js');      


}

The js code is in a folder named js and in a file named main.js

setInterval(() => {
    d = new Date(); //object of date()
    hr = d.getHours();
    min = d.getMinutes();
    sec = d.getSeconds();
    hr_rotation = 30 * hr + min / 2; //converting current time
    min_rotation = 6 * min;
    sec_rotation = 6 * sec;
  
    hour.style.transform = `rotate(${hr_rotation}deg)`;
    minute.style.transform = `rotate(${min_rotation}deg)`;
    second.style.transform = `rotate(${sec_rotation}deg)`;
}, 1000);
<?php

function childtheme_parent_styles() {
    wp_enqueue_style( 'parent', get_template_directory_urI().'/css/style.css' );
    function mytheme_files() {
        wp_enqueue_style('mytheme_main_style', get_stylesheet_uri());
        wp_register_script('main-js', get_stylesheet_directory_uri() . '/js/main.js');
    }

    add_action( 'wp_enqueue_scripts', 'childtheme_parent_styles');

    wp_register_script('main-js',get_stylesheet_directory_uri(). '/js/main.js');

}

add_action( 'wp_enqueue_scripts', 'pr_scripts_styles' );
}

function pr_scripts_styles() {
    if( is_page(507) ) {
        wp_enqueue_script( 'main-js');
    }

Will check the dev tools.

I am battling with a problem for months now, that my code will not execute or show the result, when I enqueue it. I have been told, that my code is fine, and that it should work. The clock is showing, so the css and html is correct, but not doing anything. Here it is:

<?php

function childtheme_parent_styles() {
 wp_enqueue_style( 'parent', get_template_directory_uri().'css/style.css' );
function mytheme_files() {
 wp_enqueue_style('mytheme_main_style', get_stylesheet_uri());
wp_register_script('main-js', get_stylesheet_directory_url() . '/js/main.js');
}

add_action( 'wp_enqueue_scripts', 'childtheme_parent_styles');

wp_register_script('main-js',get_stylesheet_directory_url().'/js/main.js');

}

add_action( 'wp_enqueue_scripts', 'pr_scripts_styles' );
}

function pr_scripts_styles() {
if( is_page(507) ) {
wp_enqueue_script( 'main-js');      


}

The js code is in a folder named js and in a file named main.js

setInterval(() => {
    d = new Date(); //object of date()
    hr = d.getHours();
    min = d.getMinutes();
    sec = d.getSeconds();
    hr_rotation = 30 * hr + min / 2; //converting current time
    min_rotation = 6 * min;
    sec_rotation = 6 * sec;
  
    hour.style.transform = `rotate(${hr_rotation}deg)`;
    minute.style.transform = `rotate(${min_rotation}deg)`;
    second.style.transform = `rotate(${sec_rotation}deg)`;
}, 1000);
<?php

function childtheme_parent_styles() {
    wp_enqueue_style( 'parent', get_template_directory_urI().'/css/style.css' );
    function mytheme_files() {
        wp_enqueue_style('mytheme_main_style', get_stylesheet_uri());
        wp_register_script('main-js', get_stylesheet_directory_uri() . '/js/main.js');
    }

    add_action( 'wp_enqueue_scripts', 'childtheme_parent_styles');

    wp_register_script('main-js',get_stylesheet_directory_uri(). '/js/main.js');

}

add_action( 'wp_enqueue_scripts', 'pr_scripts_styles' );
}

function pr_scripts_styles() {
    if( is_page(507) ) {
        wp_enqueue_script( 'main-js');
    }

Will check the dev tools.

Share Improve this question edited Sep 16, 2021 at 18:14 Tom J Nowell 61k7 gold badges79 silver badges148 bronze badges asked Sep 16, 2021 at 15:44 Spencer HalsteadSpencer Halstead 156 bronze badges 7
  • Oh, thank you so much, I will let you know tommorow if it works. – Spencer Halstead Commented Sep 16, 2021 at 17:23
  • 1 Is the PHP code the entire code? There's a missing } at the end, also where is the code? Which file is it in. If there is an error it will show in your PHP error log, have you checked? And when you say the code doesn't run, do you mean it fails? Or the page crashes? Or there's an error? Can you be more specific? – Tom J Nowell Commented Sep 16, 2021 at 18:11
  • 2 Use the edit link underneath the tags in your question to update it, rather than posting updated code as the solution to your question. This isn't a discussion forum so people will see you responded to your own question and assume you were posting the solution – Tom J Nowell Commented Sep 16, 2021 at 18:13
  • 1 I also strongly recommend indenting the code correctly to avoid problems, there are a number of problems that are hidden by the lack of indentation – Tom J Nowell Commented Sep 16, 2021 at 18:14
  • 1 I updated the last snippet you posted and added indentation and it's clear there are major PHP syntax issues, such as nested functions, brackets that don't match up ( { and } ), and hooks that add themselves inside themselves so they would never run, and functions that are started but never finished – Tom J Nowell Commented Sep 16, 2021 at 18:17
 |  Show 2 more comments

1 Answer 1

Reset to default 0

There are two errors I see in your code. First is that you've written get_stylesheet_directory_url() instead of get_stylesheet_directory_uri(). Switch the L for an I and that will probably fix it.

Second, for the JS file you're adding a slash before the folder, and for the CSS you're not. Once you fix the URL/I issue, you may still need to remove the /

Update I've reviewed and updated your enqueue styles. There were a number of issues, but I've tightened it up, and this will load your styles and scripts:

function childtheme_parent_styles() {
    wp_enqueue_style('parent', get_template_directory_uri().'/css/style.css' );
    wp_enqueue_style('mytheme_main_style', get_stylesheet_uri());
    wp_register_script('main-js', get_stylesheet_directory_uri() . '/js/main.js');
    if( is_page(507) ) {
        wp_enqueue_script( 'main-js');
    }
}

add_action( 'wp_enqueue_scripts', 'childtheme_parent_styles');

Good luck!

本文标签: javascriptMy code will not execute in wordpresseven though I39ve been told the code is fine