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 | Show 2 more comments1 Answer
Reset to default 0There 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
版权声明:本文标题:javascript - My code will not execute in wordpress, even though I've been told the code is fine 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741313946a2371805.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
}
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:11edit
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{
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