Php 未调用wp_排队_脚本
我在StackOverflow和其他网站上看到了很多关于为什么“wp_enqueue_脚本”不起作用的问题,但这些都不是我想要的 我想导入一个脚本到我的Wordpress主题,我复制了一个从主要开发者Wordpress网站剪下的代码 我的'functions.php'中有以下代码,输出为'output 1OUTPUT 4'。 “wpdocs\u theme\u name\u scripts()”似乎从未得到调用Php 未调用wp_排队_脚本,php,wordpress,Php,Wordpress,我在StackOverflow和其他网站上看到了很多关于为什么“wp_enqueue_脚本”不起作用的问题,但这些都不是我想要的 我想导入一个脚本到我的Wordpress主题,我复制了一个从主要开发者Wordpress网站剪下的代码 我的'functions.php'中有以下代码,输出为'output 1OUTPUT 4'。 “wpdocs\u theme\u name\u scripts()”似乎从未得到调用 /** * Proper way to enqueue scripts and s
/**
* Proper way to enqueue scripts and styles.
*/
function wpdocs_theme_name_scripts() {
echo "OUTPUT 2";
wp_enqueue_script( 'script-name', get_template_directory_uri() . '/js/main.js', array(), '1.0.0', true );
echo "OUTPUT 3";
}
echo "OUTPUT 1";
add_action( 'wp_enqueue_scripts', 'wpdocs_theme_name_scripts' );
echo "OUTPUT 4";
在header.php中使用wp_head()函数
请注意,wpdocs\u theme\u name\u scripts()
将不会在echo“OUTPUT 1”之后调用代码>和前回显“输出4”代码>
add\u action()
只会注册一个回调,当遇到wp\u head()
时会运行该回调。你可以在中找到更多的细节
您是否在主题中添加了wp\u head()
?它通常添加在
标记前面的header.php
中,类似于:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title><?php wp_title(); ?></title>
<?php wp_head(); ?>
</head>
对于任何在管理页面中遇到此问题的人,请使用admin\u enqueue\u脚本
钩子而不是wp\u enqueue\u脚本
钩子
.替换wp\u enqueue\u脚本('script name',get\u template\u directory\u uri()。/js/main.js',array(),'1.0.0',true)时是否看到任何更改代码>使用wp\u enqueue\u脚本('scriptname',get\u template\u directory\u uri()。/js/main.js')代码>?没有任何区别,我添加了调试“echo”输出仍然是一样的。mytheme_scripts()甚至没有被调用请解释您的答案哦,在添加
之后非常感谢您的帮助
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title><?php wp_title(); ?></title>
<?php wp_head(); ?>
</head>