dongtangu8403 2014-01-22 17:06
浏览 62

为什么在WP主题中,如果我从functions.php加载它,那么SlideShow(JQuery FlexSlider)无法工作,但是如果我从footer.php文件加载它就可以工作?

I have the following situation:

I am carrying this pure HTML theme (that use BootStrap CSS framework) into a WordPress theme.

This is the original pure HTML demo theme (I have the entire source code because it is a downlodable example): http://www.html.it/guide/img/bootstrap/demo/home.html

And this is my personal WP theme on wich I am working on: http://onofri.org/WP_BootStrap/

As you can see the only difference from the original demo website and my personal WP theme is that in the second one the SlideShow in the header (made using the flexslider JQuery plugin) is not shown.

I have do the following operations:

1) In my footer.php file I have disabled the JavaScript loading commenting them and I call wp_footer(), in this way:

    <!--
    <script src="http://code.jquery.com/jquery.js"></script>
    <script src="/wordpress/wp-content/themes/AsperTheme/assets/bootstrap/js/bootstrap.min.js"></script>
    <script src="/wordpress/wp-content/themes/AsperTheme/assets/plugins/flexslider/jquery.flexslider.js"></script>
    <script src="/wordpress/wp-content/themes/AsperTheme/assets/plugins/fancybox/jquery.fancybox.pack.js"></script>
    <script src="/wordpress/wp-content/themes/AsperTheme/assets/js/scripts.js"></script>
    -->
    <?php wp_footer(); ?>
</body>

2) I have insert the following code into my functions.php file to load all the previous JavaScript using the WP standard way:

/* Function automatically executed by the hook 'load_java_scripts':
 * 1) Load all my JavaScripts
 */
function load_java_scripts() {

    // Load JQuery:
    wp_enqueue_script('jquery');
    // Load FlexSlider JavaScript
    wp_enqueue_script('flexSlider-js', get_template_directory_uri() . '/assets/plugins/flexslider/jquery.flexslider.js', array('jquery'), 'v2.1', true);
    // Load bootstrap.min.js:
    wp_enqueue_script('bootstrap.min-js', get_template_directory_uri() . '/assets/bootstrap/js/bootstrap.min.js', array('jquery'), 'v3.0.3', true);

    // Load FancyBox:
    wp_enqueue_script('fancy-js', get_template_directory_uri() . '/assets/plugins/fancybox/jquery.fancybox.pack.js', array('jquery'), 'v2.1.5', true);
    // Load scripts.js:
    wp_enqueue_script('myScripts-js', get_template_directory_uri() . '/assets/js/scripts.js', array('jquery'), '1.0', true);
    // Load Modernizer:
    wp_enqueue_script('myodernizer-js', get_template_directory_uri() . '/assets/js/modernizr.custom.js', array('jquery'), '2.6.2', true);

}
add_action('wp_enqueue_scripts', 'load_java_scripts');

As you can see I set all scripts depend on JQuery so JQuery is loaded first.

But as you can see the jquery and\or the jquery.flexslider.js can't work.

The strange thing is that I change the end of my footer.php file in this way the SlideShow is correctly shown:

    <script src="http://code.jquery.com/jquery.js"></script>
    <script src="/wordpress/wp-content/themes/AsperTheme/assets/plugins/flexslider/jquery.flexslider.js"></script>

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

So what am I missing? What could be the problem?

Looking at the source code of my website (into FireFox) it seems to me that all the JavaScript is loaded but can't work, the SlideShow not appear.

What is the difference?

  • 写回答

1条回答 默认 最新

  • dtt27783 2014-01-22 23:23
    关注

    WordPress runs jQuery in nonConflict mode, you'll have to replace all $ by jQuery.

    Example:

    $('.flexslider').height();
    

    Becomes

    jQuery('.flexslider').height();
    

    WordPress codex: http://codex.wordpress.org/Function_Reference/wp_enqueue_script#jQuery_noConflict_Wrappers

    评论

报告相同问题?

悬赏问题

  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行