我正在製作一個WordPress主題,我是新來的WordPress,但對HTML,CSS,JavaScript,jQuery和PHP瞭解很多。如何加載插件js文件到自定義WordPress主題?
問題是我創建的主題不是加載插件js文件。
在這裏,我的頭代碼:
<!DOCTYPE HTML>
<html>
<head>
<title>
<?php
wp_title('|','true','right');
bloginfo('name');
?>
<meta charset="<?php bloginfo('charset'); ?>">
<meta name="viewport" content="width=device-width">
</title>
<link rel="pingback" href="<?php bloginfo('pingback_url'); ?>">
<link rel="stylesheet" type="text/css" href="<?php echo bloginfo('template_url')?>/css/reset.css" />
<link rel="stylesheet" type="text/css" href="<?php echo bloginfo('template_url')?>/css/bootstrap.min.css" />
<link rel="stylesheet" type="text/css" href="<?php echo bloginfo('template_url')?>/css/grid.css" />
<link rel="stylesheet" type="text/css" href="<?php echo bloginfo('template_url')?>/style.css" />
<script type="text/javascript" src="<?php echo bloginfo('template_url')?>/js/jquery.js"></script>
<script type="text/javascript" src="<?php echo bloginfo('template_url')?>/js/bootstrap.min.js"></script>
<?php
wp_head();
?>
</head>
<body>
<div class="container">
<div class="main-nav row" id="show-nav">
<a href="javascript:;">Navigation</a>
</div>
<div class="main-nav row" id="close-nav">
<a href="javascript:;">Close Navigation</a>
</div>
<div class="row nav-bar">
<?php
wp_nav_menu(array('container_class'=>'main-nav','container'=>'nav'));
?>
</div>
我正在手動裝載jQuery的,但我試圖用WordPress的功能加載jQuery的。
也試過這個代碼,
if (!is_admin()) add_action("wp_enqueue_scripts", "my_jquery_enqueue", 11);
function my_jquery_enqueue() {
wp_deregister_script('jquery');
wp_register_script('jquery', "http" . ($_SERVER['SERVER_PORT'] == 443 ? "s" : "") . "://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js", false, null);
wp_enqueue_script('jquery');
}
,但沒有工作。我已經嘗試了很多插件,但只是插件的js文件沒有加載,所以沒有插件工作。
我試過了metaslider插件和Easy Image Gallery插件。由於js文件,兩者都可以在Wordpress默認主題中使用,但不適用於我的。
將腳本註冊爲jquery可能會與構建到wordpress中的默認腳本名稱發生衝突。我會考慮更改enqueue_script函數名稱,使其不在列表中。 https://codex.wordpress.org/Function_Reference/wp_register_script –