简体   繁体   English

用wordpress制作Javascript幻灯片

[英]Making a Javascript slideshow with wordpress

I'm making my first steps learning to code. 我正在第一步学习编码。 I made some courses about html, css, javascript, php and MySql, and now, I decided to continue learning from the practice while I build a Wordpress child theme. 我开设了一些有关html,css,javascript,php和MySql的课程,现在,我决定在构建Wordpress子主题时继续从实践中学习。

The thing is that I am building an image slideshow. 关键是我正在构建图像幻灯片。 And to start understanding how to make it I found this: http://www.w3schools.com/w3css/w3css_slideshow.asp . 为了开始理解如何做到这一点,我发现了这一点: http : //www.w3schools.com/w3css/w3css_slideshow.asp

It's a simple and useful slideshow. 这是一个简单而有用的幻灯片。 It's really nice! 这太好了! But there are some problems if I want to implement it in Wordpress. 但是,如果我想在Wordpress中实现它,则会遇到一些问题。

I made a post type and using the Advanced Custom Fields plugin I created a Repeater Field. 我输入了帖子类型,并使用“高级自定义字段”插件创建了“转发器字段”。 So that code would be something like this in Wordpress: 因此该代码在Wordpress中将类似于以下内容:

<?php get_header(); ?>

<script>

var slideIndex = 1;
showDivs(slideIndex);

function plusDivs(n) {
  showDivs(slideIndex += n);
}

function showDivs(n) {
  var i;
  var x = document.getElementsByClassName("mySlides");
  if (n > x.length) {slideIndex = 1}
  if (n < 1) {slideIndex = x.length}
  for (i = 0; i < x.length; i++) {
     x[i].style.display = "none";
  }
  x[slideIndex-1].style.display = "block";
}

</script>


<?php


if( have_rows('image') ):

    while ( have_rows('image') ) : the_row();


                if( get_sub_field('subimage') ) :

            echo '<div class="mySlides">' . get_sub_field('subimage') . '</div>';
          endif;

    endwhile;


endif;

?>

<a class="home-btn-left" onclick="plusDivs(-1)">❮</a>
<a class="home-btn-right" onclick="plusDivs(1)">❯</a>


</div>

<?php get_footer(); ?>

It works almost perfect! 它几乎完美! But now I have two problems: 但是现在我有两个问题:

1) Once I load my page to see the template. 1)一旦我加载页面以查看模板。 I see the first image overlaying the last image. 我看到第一个图像覆盖了最后一个图像。 I don't know why: 我不知道为什么:

在此处输入图片说明

2) At the end of the slideshow there is an empty slide. 2)在幻灯片的末尾有一张空的幻灯片。 It looks like I have an empty subfield but no, I don't have empty subfields. 看来我有一个空子字段,但没有,我没有空子字段。 I don't know why there is an empty slide. 我不知道为什么没有一张空的幻灯片。

Do you have some recommendation? 你有什么建议吗?

Thank you 谢谢

I think the problem is the last closing DIV </div> just before your get_footer() . 我认为问题是在get_footer()之前的最后一个关闭DIV </div> It's a simple html markup error that causes the browser to fail. 这是一个简单的html标记错误,导致浏览器失败。 Your code should look like this: 您的代码应如下所示:

<?php get_header(); ?>

<script>

var slideIndex = 1;

function plusDivs(n) {
  showDivs(slideIndex += n);
}

function showDivs(n) {
  var i;
  var x = document.getElementsByClassName("mySlides");
  if (n > x.length) {slideIndex = 1}
  if (n < 1) {slideIndex = x.length}
  for (i = 0; i < x.length; i++) {
     x[i].style.display = "none";
  }
  x[slideIndex-1].style.display = "block";
}

</script>


<?php

if( have_rows('image') ):
  while ( have_rows('image') ) : the_row();
    if( get_sub_field('subimage') ) :
      echo '<div class="mySlides">' . get_sub_field('subimage') . '</div>';
    endif;
  endwhile;
endif;

?>

<a class="home-btn-left" onclick="plusDivs(-1)">❮</a>
<a class="home-btn-right" onclick="plusDivs(1)">❯</a>

<script>
  showDivs(slideIndex);
</script>

<?php get_footer(); ?>

Please let me know if this doesn't fix your problem. 如果这不能解决您的问题,请告诉我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM