在特定页面的Wordpress上运行特定的Js [英] Run Specific Js on Specific page Wordpress

查看:64
本文介绍了在特定页面的Wordpress上运行特定的Js的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在特定页面上运行特定js.即:wwww.custom.com/english/

I want to run a specific js on a specific page. I.e: wwww.custom.com/english/

我尝试了以下两个代码(header.php和functions.php),但是它们都不起作用.

I've tried the following two codes (header.php and functions.php) but none of them work.

代码1:

<script>
if(location.pathname=="/english/") 
 script.src = /js/custom.js;
</script>

代码2:

function my_scripts() {
    if( is_page( array( 'about-us', 'contact', 'management' ) ){
        wp_enqueue_script( 'script-name', 'path/to/example.js', array(), '1.0.0', true );
    }
}
add_action( 'wp_enqueue_scripts', 'my_scripts' );

一个标题根本不显示,也没有错误.functions.php是语法错误.

The header one does not show at all, no error either. The functions.php is a syntax error.

有什么建议吗?很有义务

Any suggestions? Much Obliged

PS:我正在使用WordPress.

PS: I'm using WordPress.

推荐答案

您的代码很棒!您只是缺少右括号:

You code is great! You are just missing a closing parenthesis:

function my_scripts() {
    if( is_page( array( 'about-us', 'contact', 'management' ) ) ){
        wp_enqueue_script( 'script-name', 'path/to/example.js', array(), '1.0.0', true );
    }
}
add_action( 'wp_enqueue_scripts', 'my_scripts' );

为了将来参考,可以在wp-config.php文件中将debug设置为true,我相信这会为您带来帮助.

For future reference, in your wp-config.php file you can set debug to true, I believe that this will pick it up.

这篇关于在特定页面的Wordpress上运行特定的Js的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆