【问题标题】:Can't get jquery and flexslider working on a new Wordpress install无法让 jquery 和 flexslider 在新的 Wordpress 安装上工作
【发布时间】:2015-02-10 08:05:57
【问题描述】:

我正在尝试向 wordpress 子主题页面添加一个简单的滑块,但无法正常工作。

滑块 html(header.php):

$args = array(
    'post_type' => 'slide'
  );

  // The Query
  $the_query = new WP_Query( $args );

  // The Loop
  if ( $the_query->have_posts() ) {
      echo '<div class="flexslider"><ul class="slides">';
      while ( $the_query->have_posts() ) {
          $the_query->the_post();
          //echo '<li><a href="#">' . the_post_thumbnail() . '</a>    </li>';//this was wrong
          echo '<li><a href="#">' . get_the_post_thumbnail() . '</a>    </li>';//this is right
      }
      echo '</ul></div>';
  } else {
      // no posts found
  }
  wp_reset_postdata();

脚本(functions.php)

function my_scripts() {
    wp_register_script( 'flexslider', get_stylesheet_directory_uri() . '/flexslider/jquery.flexslider.js', array( 'jquery' ), '1.0', true );
wp_register_script( 'slide', get_stylesheet_directory_uri() . '/flexslider/flexscript.js', array( 'jquery' ), '1.0', true );

if ( is_front_page() ) { 
    wp_enqueue_script( 'flexslider' );
}

}

add_action( 'wp_enqueue_scripts', 'my_scripts' );

jquery.flexislider.js 是必需的js文件。 flexiscript.js 包含以下代码:

jQuery(document).ready(function() {
jQuery('.flexslider').flexslider();
});

我的标题中也包含 css

<link rel="stylesheet" href="<?php echo get_stylesheet_directory_uri(); ?>/flexslider/flexslider.css" type="text/css">

【问题讨论】:

  • 你有任何控制台错误..??
  • 只是这个:The "fb-root" div has not been created, auto-creating 我怀疑这与滑块或 jquery 有什么关系

标签: jquery wordpress


【解决方案1】:

您没有将 flexscript.js 文件排入队列:

wp_enqueue_script('slide');

【讨论】:

  • 嗨,谢谢。这实际上做了一些事情。它添加了滑块的控件......但仍然有问题。这是一个链接:limitless.smoige.com
  • 我以为wp_register_script('jquery');是wordpress jquery的注册方式。
  • 是的,wp_register_script() 缺少参数 2。您不必注册 jQuery。如果 jQuery 在其中一个依赖项中,WordPress 会自动为您加入队列;)
  • 仍然显示两个图像而不是滑块。我应该注册样式表而不是将其添加到 header.php 中吗?
  • 看起来滑块正在工作,但缺少一些 css 样式。最好注册 css,但不是强制性的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-08
  • 2017-04-05
  • 2015-07-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多