我只需要在我的联系人页面中添加一些谷歌地图的scrips,我不想在所有页面上填充它。我也不愿意创建自定义头,所以我想有没有办法只在联系人页面上填充js文件?目前,我正在使用此代码将javascript添加到主题中
//Load Scripts
function load_scripts(){
wp_deregister_script(\'jquery\'); // De-register Existing jquery
wp_enqueue_script(\'jquery\', \'http://code.jquery.com/jquery.js\', \'\', \'\', true);
wp_enqueue_script(\'bootstrap-jquery\', get_template_directory_uri().\'/assets/js/bootstrap.js\', \'\', \'\', true);
}
但正如我所说,我有另一个文件叫做
googlemap.js 您能告诉我是否可以将其添加到此代码的唯一联系人页面吗?另一点是Wordpress在正文末尾(标记之前)添加了此代码
I do not know how and why! 但在我的特定情况下,我需要将代码添加到!你能告诉我怎么做吗
谢谢
最合适的回答,由SO网友:Sisir 整理而成
使用条件is_page()
用于在特定页面上加载脚本。
Here Is The Modified Version of Your Function:
//Load Scripts
function load_scripts(){
wp_deregister_script(\'jquery\'); // De-register Existing jquery
wp_register_script(\'jquery\', \'http://code.jquery.com/jquery.js\', \'\', \'\', true);
wp_enqueue_script( \'jquery\' );
wp_register_script(\'bootstrap-jquery\',get_template_directory_uri().\'/assets/js/bootstrap.js\', array( \'jquery\' ), \'\', true);
wp_enqueue_script( \'bootstrap-jquery\' );
// register the script
wp_register_script( \'my-script\', \'URL to Script\' ); // by default script will load to head
// conditionally load page
if (is_page( \'Contact\' )) {
wp_enqueue_script(\'my-script\');
}
}
SO网友:Por
这里有一些窍门给你
注:
wp_register_script(); // is only register js file, it is not running
wp_deregiser_script(); // is completely remove js file, it is not nice method is to use in page condition check
//Run Js
wp_enqueue_script(); // is use only your register js file,
wp_dequeue_script(); // this function make not to run js file, it is nice method to use in page codition check
在您的功能中。php文件
function js_library() {
//1. Method
if ( is_front_page() ) {
//wp_dequeue_script();
}
//2. Method
if ( is_page(120)) { //120 is page id, you can get page id using using $post->ID or get_the_ID
//wp_dequeue_script();
}
if ( is_page($post->ID) == 120 ) { //120 is your page id
//wp_dequeue_script();
}
if ( is_page(\'about-us\') ) { //about-us is using page slug, you can get page slug http://yourdomain.com/about-us
//wp_dequeue_script();
}
$remove_js_pages = array(\'about-us\', \'contact-us\', \'service\'); // for multi page slugs
if ( is_page($remove_js_pages) ) { //remember you can get page slug from your page url ;-)
//wp_dequeue_script();
}
//Using page name
$pagename = get_query_var(\'pagename\');
if ( !$pagename && $id > 0 ) {
// If a static page is set as the front page, $pagename will not be set. Retrieve it from the queried object
$post = $wp_query->get_queried_object();
$pagename = $post->post_name;
}
if ( is_page() == $pagename ) {
//wp_dequeue_script();
}
}