我正在将Bootstrap主题转换为Wordpress但我无法让我的脚本加载并运行

时间:2016-04-25 08:07:23

标签: javascript php jquery wordpress twitter-bootstrap

这是我在GitHub上的主题为https://github.com/RyanChrist4/ryanstrap

的回购

我所做的是从头部删除脚本标记,并使用wp_register_script函数和wp_enqueue_script函数将它们放入functions.php文件中。我已经查看了其他人类similair问题的答案,并尝试将其格式化,但我仍然无法运行脚本。我是Web开发的新手,也是Wordpress的新手。此外,在标题中,有一个调用多个函数的脚本标记,但是在控制台中出现“未捕获的参考错误”的错误。

这是我的functions.php文件中的代码:

    <?php
//jQuery Insert From Google
if (!is_admin()) add_action("wp_enqueue_scripts", "my_jquery_enqueue");
function my_jquery_enqueue() {
   wp_deregister_script('jquery');
   wp_register_script('jquery', "http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js", false, null);
   wp_enqueue_script('jquery');
}
//Launches all of the scripts below
add_action("wp_enqueue_scripts", "launch_scripts");
function launch_scripts() {
    wp_enqueue_script('the-flexslider', get_template_directory_uri() . '/assets/js/jquery.flexslider-min.js');
    wp_enqueue_script('the-fitvids', get_template_directory_uri() . '/assets/js/jquery.fitvids.js');
    wp_enqueue_script('the-smoothscroll', get_template_directory_uri() . '/assets/js/jquery.smooth-scroll.min.js');
    wp_enqueue_script('the-backstretch', get_template_directory_uri() . '/assets/js/jquery.backstretch.min.js');
    wp_enqueue_script('the-bootstrap', get_template_directory_uri() . '/assets/js/bootstrap.js');
    wp_enqueue_script('the-bootstrapmin', get_template_directory_uri() . '/assets/js/bootstrap.min.js');
    wp_enqueue_script( 'the-gmaps', get_template_directory_uri() . '/http://maps.google.com/maps/api/js', array(), '20120206', true );
    wp_enqueue_script('the-mgmaps', get_template_directory_uri() . '/assets/js/gmaps.js');
    wp_enqueue_script('the-script', get_template_directory_uri() . '/assets/js/script.js');
}
?>

以下是我的header.php文件的内容:

    <!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" /> <!-- Its not valid html but it works -->
        <title><?php bloginfo( 'title' ); ?></title>
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <meta name="description" content="A very good admin theme" />
        <meta name="author" content="AHthemes" />
        <!--<script>menu(); backstretch(); slider(); contentslider(); map(); panels(); blogposts();</script>-->
        <!-- Le styles -->
        <link rel="stylesheet" type="text/css" href="<?php bloginfo('stylesheet_url'); ?>"media="screen" />
        <!-- HTML5 shim, for IE6-8 support of HTML5 elements -->
        <!--[if lt IE 9]>
        <script src="assets/js/html5shiv.js"></script>
        <script src="assets/js/respond.min.js"></script>
        <![endif]-->
        <!--[if gte IE 9]>
        <style type="text/css">
            .gradient { filter: none; }
        </style>
        <![endif]-->
        <!-- Fav and touch icons -->
        <link rel="apple-touch-icon-precomposed" sizes="144x144" href="assets/ico/apple-touch-icon-144-precomposed.png">
        <link rel="apple-touch-icon-precomposed" sizes="114x114" href="assets/ico/apple-touch-icon-114-precomposed.png">
        <link rel="apple-touch-icon-precomposed" sizes="72x72" href="assets/ico/apple-touch-icon-72-precomposed.png">
        <link rel="apple-touch-icon-precomposed" href="assets/ico/apple-touch-icon-57-precomposed.png">
        <link rel="shortcut icon" href="assets/ico/favicon.png">
<?php
  wp_head();
?>
    </head>
    <body>
      <div class="nav-section">
          <ul class="nav">
              <li><a href="#home" class="scroll home">Home</a></li>
              <li><a href="#about" class="scroll about">About</a></li>
              <li><a href="#work" class="scroll work">Work</a></li>
              <li><a href="#contact" class="scroll contact">Contact</a></li>
          </ul>
      </div>

3 个答案:

答案 0 :(得分:0)

如果我能相信文档,您不需要先注册脚本:

  

wp_enqueue_script - 如果提供$ src(不覆盖)并将其排队,则注册脚本。

了解更多here

<?php
//jQuery Insert From Google
if (!is_admin()) add_action("wp_enqueue_scripts", "my_jquery_enqueue");
function my_jquery_enqueue() {
   wp_deregister_script('jquery');
   wp_register_script('jquery', "http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js", false, null);
   wp_enqueue_script('jquery');
}
//Launches all of the scripts below
add_action("wp_enqueue_scripts", "launch_scripts");
function launch_scripts() {
    wp_enqueue_script('the-flexslider', get_template_directory_uri() . '/assets/js/jquery.flexslider-min.js');
    wp_enqueue_script('the-fitvids', get_template_directory_uri() . '/assets/js/jquery.fitvids.js');
    wp_enqueue_script('the-smoothscroll', get_template_directory_uri() . '/assets/js/jquery.smooth-scroll.min.js');
    wp_enqueue_script('the-backstretch', get_template_directory_uri() . '/assets/js/jquery.backstretch.min.js');
    wp_enqueue_script('the-bootstrap', get_template_directory_uri() . '/assets/js/bootstrap.js');
    wp_enqueue_script('the-bootstrapmin', get_template_directory_uri() . '/assets/js/bootstrap.min.js');
    wp_enqueue_script( 'the-gmaps', '//maps.google.com/maps/api/js');
    wp_enqueue_script('the-mgmaps', get_template_directory_uri() . '/assets/js/gmaps.js');
    wp_enqueue_script('the-script', get_template_directory_uri() . '/assets/js/script.js');
}
?>

答案 1 :(得分:0)

请尝试这一行

wp_enqueue_script( 'the-gmaps', get_template_directory_uri() . '/http://maps.google.com/maps/api/js', array(), '20120206', true );

答案 2 :(得分:0)

这听起来像是一个JavaScript问题,由WordPress加剧。

一般来说,当有人提及&#34;未捕获的参考错误&#34;它与你的脚本有关,取决于某些东西,而那个东西还没有加载。

那是我的预感。 为此,您的代码应更改为以下内容 - 我清理了您的代码: - )

还修复了一些代码错误,你的一个外部脚本被严重加载(gmap的东西)。

<?php

    // Replace Core jQuery version with 1.7.1 (Bootstrap Theme Requirement)
    // WordPress 4.5 includes 1.12.3
    // Only do this when we are not within the Administration area of WordPress.
    if (!is_admin()) {
        add_action("wp_enqueue_scripts", "replace_jquery");
    }

    function replace_jquery() {
        wp_deregister_script('jquery');
        wp_register_script('jquery', "//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js", false, '1.7.1', false);
        wp_enqueue_script('jquery');
    }

    // Load all of the scripts below, on every single page. :-\
    add_action("wp_enqueue_scripts", "launch_scripts");
    function launch_scripts() {
        $internal_path = get_template_directory_uri() . '/assets/js/';
        wp_enqueue_script('the-flexslider', $internal_path . 'jquery.flexslider-min.js', array('jquery'));
        wp_enqueue_script('the-fitvids', $internal_path . 'jquery.fitvids.js', array('jquery'));
        wp_enqueue_script('the-smoothscroll', $internal_path . 'jquery.smooth-scroll.min.js', array('jquery'));
        wp_enqueue_script('the-backstretch', $internal_path . 'jquery.backstretch.min.js', array('jquery'));
        wp_enqueue_script('the-bootstrap', $internal_path . 'bootstrap.js', array('jquery'));
        wp_enqueue_script('the-bootstrapmin', $internal_path . 'bootstrap.min.js', array('jquery'));
        wp_enqueue_script('the-gmaps', '//maps.google.com/maps/api/js', array('jquery'), '20120206', true);
        wp_enqueue_script('the-mgmaps', $internal_path . 'gmaps.js', array('jquery'));
        wp_enqueue_script('the-script', $internal_path . 'script.js', array('jquery'));
    }
?>