Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/260.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/kubernetes/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在wordpress中有条件地加载脚本_Javascript_Php_Wordpress - Fatal编程技术网

Javascript 在wordpress中有条件地加载脚本

Javascript 在wordpress中有条件地加载脚本,javascript,php,wordpress,Javascript,Php,Wordpress,我可以在我的child theme functions.php文件中使用以下命令在wordpress中加载(排队)脚本: function my_assets() { wp_enqueue_script( 'about', get_stylesheet_directory_uri() . '/js/about.js', array( 'jquery' ), '1.0' , true ); } add_action( 'wp_enqueue_scripts', 'my_assets'

我可以在我的child theme functions.php文件中使用以下命令在wordpress中加载(排队)脚本:

function my_assets() {

    wp_enqueue_script( 'about', get_stylesheet_directory_uri() . '/js/about.js', array( 'jquery' ), '1.0' , true );
}

add_action( 'wp_enqueue_scripts', 'my_assets' );
加载不是问题,脚本可以工作,但当我想有条件地将其加载到页面上时,它不适用于此代码(在同一个文件中,并直接位于上述代码的下方):

脚本仍然出现在所有页面上。我尝试使用页面名称slug,我将wp_dequeue_脚本更改为admin_dequeue_脚本,但没有任何效果

不过值得注意的是,当我在管理中单击页面以获取页面ID号时,地址栏中有“post”一词,即使我在“Pages.wp admin/post.php?post=490&action=edit

哈哈,看起来很直截了当,我错过了什么


谢谢

您可以提前返回,而不是根据特定条件执行另一个函数将脚本出列:

function my_assets() {
    if ( ! is_page( 490 ) ) {
        return;
    }

    wp_enqueue_script( 'about', get_stylesheet_directory_uri() . '/js/about.js', array( 'jquery' ), '1.0', true );
}

add_action( 'wp_enqueue_scripts', 'my_assets' );

现在,
wp\u enqueue\u script
函数仅在ID 490页上被调用。

您可以提前返回,而不是根据特定条件首先将脚本排队:

function my_assets() {
    if ( ! is_page( 490 ) ) {
        return;
    }

    wp_enqueue_script( 'about', get_stylesheet_directory_uri() . '/js/about.js', array( 'jquery' ), '1.0', true );
}

add_action( 'wp_enqueue_scripts', 'my_assets' );

现在,
wp\u enqueue\u script
函数只在ID490页上被调用。

如何/在哪里调用
deregister\u about\u javascript()
function?我知道你已经将函数声明放在了同一个文件中,但你实际上是如何调用该函数的?@Andy我以前考虑过这个lol,但我没有。我“排队”的代码脚本在没有被调用的情况下工作,所以我想它一定是同一件事。我错了?第一个函数被调用是因为你已经将它添加到
wp\u enqueue\u脚本
操作中,所以当WordPress触发该操作时,它调用你的函数。我已经发布了一个答案,它不再需要第二个函数。你是怎么做的调用
deregister\u about\u javascript()
函数?我知道你已经将函数声明放在了同一个文件中,但你实际上是如何调用该函数的?@Andy我以前考虑过这个问题,现在不是了。我“排队”的代码脚本在没有被调用的情况下工作,所以我想它一定是同一件事。我错了?第一个函数被调用是因为你已经将它添加到
wp\u enqueue\u脚本
操作中,所以当WordPress启动该操作时,它会调用你的函数。我已经发布了一个答案,它不再需要第二个函数了。哈哈,我真的感觉到了我喜欢你的回答,因为它使我的回答更有效:)非常感谢你,我真的觉得自己是个完全的智障。老实说,我没有采取任何行动。我喜欢你的回答,因为它使它更有效率:)非常感谢