I am making my first steps learning to code. I made some courses on Internet and now I'm continue learning from the experience while I build a Wordpress child theme.
The thing is that I'm making an image slideshow. I found this great and simple one in w3schools: http://www.w3schools.com/w3css/tryit.asp?filename=tryw3css_slideshow_self.
It's really fine but there are some problems if I want to use it in Wordpress.
I have a post type template with some fields that I build using the Advanced custom fields app. For the moment I have 10 images fields to make the slideshow.
So it means that this:
<!DOCTYPE html>
<html>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://www.w3schools.com/lib/w3.css">
<body>
<h2 class="w3-center">Manual Slideshow</h2>
<div class="w3-content" style="max-width:800px;position:relative">
<img class="mySlides" src="img_fjords.jpg" style="width:100%">
<img class="mySlides" src="img_lights.jpg" style="width:100%">
<img class="mySlides" src="img_mountains.jpg" style="width:100%">
<img class="mySlides" src="img_forest.jpg" style="width:100%">
<a class="w3-btn-floating" style="position:absolute;top:45%;left:0" onclick="plusDivs(-1)">❮</a>
<a class="w3-btn-floating" style="position:absolute;top:45%;right:0" onclick="plusDivs(1)">❯</a>
</div>
<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>
</body>
</html>
Becomes this in 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>
<div class="mySlides"><?php the_field("image"); ?></div>
<div class="mySlides"><?php the_field("image_2"); ?></div>
<div class="mySlides"><?php the_field("image_3"); ?></div>
<div class="mySlides"><?php the_field("image_4"); ?></div>
<div class="mySlides"><?php the_field("image_5"); ?></div>
<div class="mySlides"><?php the_field("image_6"); ?></div>
<div class="mySlides"><?php the_field("image_7"); ?></div>
<div class="mySlides"><?php the_field("image_8"); ?></div>
<div class="mySlides"><?php the_field("image_9"); ?></div>
<div class="mySlides"><?php the_field("image_10"); ?></div>
<a class="home-btn-left" onclick="plusDivs(-1)">❮</a>
<a class="home-btn-right" onclick="plusDivs(1)">❯</a>
</div><!-- .content-area -->
<a class="homelink" href="http://localhost/wordpress/">Sarah Morris</a>
<?php get_footer(); ?>
Now the problem is that I have to upload 10 images in all my posts. If one day I upload only 8 images for example, I will see the 8 images but I will also see two blank spaces because of the other image fields that I'm not using.
After some days trying to find a solution, I found that I have two options to do it:
1) Using the repeater field of the Advanced custom fields plugin.
2) Using this code:
<?php if(get_field("image") != ""): ?>
<div class="mySlides"><?php the_field("image"); ?></div>
<?php endif; ?>
The repeater field is a premium feature. I don't want to pay for a plugin, I prefer to learn how to make by my self so I decided to use the second option.
Now the problem is that the second option doesn't work correctly. It doesn't make what I want: To make an Slideshow without blank spaces when I'm using less than 10 images.
I know that this code is really near from the objective that I want to achieve. Do you have some suggestion?

