javascript - 在wordpress中导入脚本
问题描述
问题
大家好,我不明白为什么我无法通过脚本寄存器在 WordPress 中输入外部文件:
如果我尝试console.log('here')
在外部文件中加载一个简单的文件,我根本无法启动它。好像没有找到文件。
也许我在声明中错了?
代码
这是注册脚本
<?php
function wp_load_styles_on_init()
{
if (!is_admin()){
/* REGISTER SCRIPT */
wp_deregister_script('jquery');
wp_register_script('jquery', get_the_theme_uri() . '/js/jquery-3.1.1.min.js', array(), null, true);
wp_register_script('bootstrap-js', get_the_theme_uri() . '/js/bootstrap-3.3.7/js/bootstrap.min.js', array('jquery'), null, true);
wp_register_script('slide-push-menu-js', get_the_theme_uri() . '/js/slide-push-menu/js/menu.js', array('jquery','bootstrap-js'), null, true);
wp_register_script('ie10-workaround-js', get_the_theme_uri() . '/js/ie10-workaround/ie10-viewport-bug-workaround.js', array('jquery','bootstrap-js'), null, true);
// wp_register_script('infinite-scroll', get_the_theme_uri() . '/js/infinite-scroll-helper.js', array('jquery'), null, true);
wp_register_script('slick', get_the_theme_uri() . '/js/slick-1.6.0/slick.min.js', array('jquery'), null, true);
wp_register_script('waypoints', get_the_theme_uri() . '/js/jquery.waypoints.min.js', array('jquery'), null, true);
wp_register_script('visible-js', get_the_theme_uri() . '/js/jquery.visible.min.js', array('jquery'), null, true);
wp_register_script('functions-js', get_the_theme_uri() . '/js/site-functions.js', array('jquery','bootstrap-js','ie10-workaround-js'), null, true);
wp_register_script('home-function', get_the_theme_uri() . '/js/home-function.js', array('jquery'),null, true);
wp_localize_script('home-function', 'ajax_post_params', array('ajax_url' => admin_url('admin-ajax.php')));
wp_register_script('load-more', get_the_theme_uri() . '/js/load-more.js', array('jquery'),null, true);
wp_localize_script('load-more', 'ajax_single_params', array('ajax_url' => admin_url('admin-ajax.php')));
wp_register_script('fancybox', get_the_theme_uri() . '/js/jquery.fancybox.min.js', array('jquery'), null, true);
wp_register_script('adv-function', get_the_theme_uri() . '/js/adv-functions.js', array('jquery'), null, true);
wp_register_script('top-banner-adv-functions', get_the_vision_theme_uri() . '/js/top-banner-adv-functions.js', array('jquery'), null, true);
wp_register_script('final-banner-adv-functions', get_the_theme_uri() . '/js/final-banner-adv-functions.js', array('jquery'), null, true);
wp_enqueue_script('jquery');
wp_enqueue_script('bootstrap-js');
wp_enqueue_script('slide-push-menu-js');
wp_enqueue_script('ie10-workaround-js');
wp_enqueue_script('waypoints');
wp_enqueue_script('visible-js');
wp_enqueue_script('functions-js');
if(is_front_page()){
wp_enqueue_script('home-function');
wp_enqueue_script('slick');
wp_enqueue_script('adv-function');
wp_enqueue_script('top-banner-adv-functions');
}
if(is_single() || is_category() || is_author()):
// wp_enqueue_script('infinite-scroll');
// wp_enqueue_script('single-function');
wp_enqueue_script('load-more'); // and this one
if(is_single() && get_query_var('post_type') != 'room'):
wp_enqueue_script('adv-function');
wp_enqueue_script('top-banner-adv-functions');
wp_enqueue_script('final-banner-adv-functions');
endif;
endif;
if(get_query_var('post_type') == 'room' || is_tax()):
wp_enqueue_script('fancybox');
wp_enqueue_script('adv-function');
wp_enqueue_script('top-banner-adv-functions');
endif;
// $queried_post_type = get_query_var('post_type');
// if ( is_single() && 'video' == $queried_post_type):
// wp_dequeue_script('infinite-scroll');
// wp_dequeue_script('single-function');
// endif;
/* REGISTER STYLE */
wp_register_style( 'bootstrap', get_the_vision_theme_uri() . '/js/bootstrap-3.3.7/css/bootstrap.min.css', array(), '', 'all' );
wp_register_style( 'ie10-workaround', get_the_vision_theme_uri() . '/js/ie10-workaround/ie10-viewport-bug-workaround.css', array('bootstrap'), '', 'all' );
wp_register_style( 'slide-push-menu', get_the_vision_theme_uri() . '/js/slide-push-menu/css/menu.css', array('bootstrap'), '', 'all' );
wp_register_style( 'fontello', get_the_vision_theme_uri() . '/css/fontello.css', array(), '', 'all' );
wp_register_style( 'slick', get_the_vision_theme_uri() . '/js/slick-1.6.0/slick.css', array(), '', 'all' );
wp_register_style( 'style', get_the_vision_theme_uri() . '/css/style.css', array(), '20180313', 'all' );
wp_register_style( 'custom', get_the_vision_theme_uri() . '/css/custom.css', array(), '', 'all' );
wp_register_style( 'fancybox', get_the_vision_theme_uri() . '/css/jquery.fancybox.min.css', array(), '', 'all' );
wp_enqueue_style('bootstrap');
wp_enqueue_style('slide-push-menu');
wp_enqueue_style('ie10-workaround');
wp_enqueue_style('tv-google-fonts', 'https://fonts.googleapis.com/css?family=Crimson+Text', false);
wp_enqueue_style('ds-google-fonts','https://fonts.googleapis.com/css?family=Droid+Serif',false);
wp_enqueue_style('amiri-google-fonts','https://fonts.googleapis.com/css?family=Amiri',false);
wp_enqueue_style('fontello');
wp_enqueue_style('slick');
wp_enqueue_style('style');
wp_enqueue_style('custom');
if(get_query_var('post_type') == 'room'):
wp_enqueue_style('fancybox');
endif;
}
}
add_action( 'wp_enqueue_scripts', 'wp_load_styles_on_init' );
有趣的线条是两条未缩进的线和下面的这条线
if(is_single() || is_category() || is_author()):
// wp_enqueue_script('infinite-scroll');
// wp_enqueue_script('single-function');
wp_enqueue_script('load-more'); // and this one
现在,我不明白这就是为什么文件 load-more.js 甚至没有被调用。
下面是文件树:
更新
我正在尝试深入,并且我正在查看该功能的get_the_theme_uri()
实际作用。在下面,您将找到相关代码:
define('MY_WORDPRESS_FOLDER', $_SERVER['DOCUMENT_ROOT']);
define('MY_THEME_FOLDER', str_replace('\\', '/', dirname(__FILE__)));
//define('MY_THEME_PATH', '/themes');
define('MY_THEME_PATH', '/' . substr(MY_THEME_FOLDER, stripos(MY_THEME_FOLDER, 'wp-content')));
function get_the_theme_uri()
{
return MY_THEME_PATH;
}