PHP& WordPress - 当我使用add_action(),wp_head()和wp_footer()函数时,为什么我的代码会中断?如何修复它?

时间:2015-06-19 19:54:30

标签: php wordpress

这些函数位于我的header.php和functions.php文件中。我包括我的header.php,index.php,footer.php和functions.php文件。我相信已经对错误进行了分区,它与functions.php中的第二个add_action以及wp_head()和wp_footer()函数有关。基本上,当我删除functions.php中的第二个add动作或整个pt_theme_js功能块时,不会发生错误(页面不加载)。

如何解决此错误及其原因?

的header.php

<!doctype html>
<html class="no-js" lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title><?php wp_title(); ?></title>
  </head>

  <?php wp_head(); ?>

  <body>
    <header class="row no-max pad main">
  <h1><a class='current' href="index.html">G</a></h1>
  <a href="" class="nav-toggle"><span></span>Menu</a>
  <nav>
    <h1 class="open"><a class='current' href="index.html">G</a></h1>
    <ul class="no-bullet">
      <li class="current parent"><a class='current' href="index.html">Portfolio</a>
        <ul class="sub-menu">
          <li><a href="item.html">Portfolio Item</a></li>
          <li><a href="item.html">Portfolio Item</a></li>
          <li><a href="item.html">Portfolio Item</a></li>
          <li><a href="item.html">Portfolio Item</a></li>
        </ul>
      </li>
      <li class="parent"><a href="blog.html">Blog</a>
        <ul class="sub-menu">
          <li><a href="single-post.html">Single Post</a></li>
          <li><a href="author.html">Author Page</a></li>
        </ul>
      </li>
      <li><a href="about.html">About</a></li>
      <li><a href="contact.html">Contact</a></li>
    </ul>
  </nav>
</header>

的index.php

<?php get_header(); ?>
<?php get_footer(); ?>

Footer.php

<div class="footer-clear"></div>
<footer class="row no-max pad">
  <ul class="social-links no-bullet">
    <li><a href="" class="icon icon-twitter"></a></li>
    <li><a href="" class="icon icon-facebook"></a></li>
    <li><a href="" class="icon icon-vimeo"></a></li>
    <li><a href="" class="icon icon-youtube"></a></li>
    <li><a href="" class="icon icon-linkedin"></a></li>
    <li><a href="" class="icon icon-github"></a></li>
    <li><a href="" class="icon icon-flickr"></a></li>
    <li><a href="" class="icon icon-google"></a></li>
    <li><a href="" class="icon icon-email"></a></li>
  </ul>            
  <p>Copyright <?php echo date('Y'); ?></p>
</footer>

    <?php wp_footer(); ?>
  </body>
</html>

的functions.php

<?php

function wpt_theme_styles() {
    wp_enqueue_style('foundation_css', get_template_directory_uri() . '/css/foundation.css');
    wp_enqueue_style('normalize_css', get_template_directory_uri() . '/css/normalize.css');
    wp_enqueue_style('main_css', get_template_directory_uri() . '/style.css');
}

add_action('wp_enqueue_scripts', 'wpt_theme_styles');

function wpt_theme_js() {
    wp_enqueue_scripts('modernizr_js', get_template_directory_uri() . '/js/modernizr.js', '', '', false);
    wp_enqueue_scripts('foundation_js', get_template_directory_uri() . '/js/foundation.min.js', array('jquery'), '', true);
    wp_enqueue_scripts('main_js', get_template_directory_uri() . '/js/app.js', array('jquery', 'foundation_js'), '', true);
}

add_action('wp_enqueue_scripts', 'wpt_theme_js');

?>

3 个答案:

答案 0 :(得分:1)

解决。

我通过使用本演练解决了它: http://www.wpbeginner.com/wp-tutorials/how-to-properly-add-javascripts-and-styles-in-wordpress/

我所做的是更改处理JS文件的wp_enqueue_scripts,并在wp_enqueue_script之前首先使用wp_register_script。 @ rubenrp81提到了将wp_enqueue_scripts更改为wp_enqueue_script之类的内容,答案就在我上面分享的链接中。

这是我生成的functions.js文件:

    XMLParser parser = new XMLParser();
    String xml = parser.getXmlFromUrl(URL); // getting XML
    Document doc = parser.getDomElement(xml); // getting DOM element

    NodeList nl = doc.getElementsByTagName(KEY_ITEM);
    // looping through all item nodes <item>
    for (int i = 0; i < nl.getLength(); i++) {
        // creating new HashMap
        HashMap<String, String> map = new HashMap<String, String>();
        Element e = (Element) nl.item(i);
        // adding each child node to HashMap key => value
        map.put(KEY_ID, parser.getValue(e, KEY_ID));
        map.put(KEY_NAME, parser.getValue(e, KEY_NAME));
        map.put(KEY_DESC, parser.getValue(e, KEY_DESC));



        // adding HashList to ArrayList
        menuItems.add(map);
    }

答案 1 :(得分:0)

更改

wp_enqueue_scriptS( [...]

通过

wp_enqueue_script( [...]

答案 2 :(得分:-1)

尝试删除结束'?&gt;'在函数文件的末尾。

不保证,但通常没有。