【发布时间】:2017-01-13 20:47:27
【问题描述】:
我正在迈出学习编程的第一步。我在互联网上做了一些课程,现在我在构建 Wordpress 儿童主题时继续从经验中学习。
问题是我正在制作图像幻灯片。我在 w3schools 中发现了这个伟大而简单的:http://www.w3schools.com/w3css/tryit.asp?filename=tryw3css_slideshow_self。
真的很好,但是如果我想在 Wordpress 中使用它会有一些问题。
我有一个帖子类型模板,其中包含一些我使用高级自定义字段应用程序构建的字段。目前我有 10 个图像字段来制作幻灯片。
所以这意味着:
<!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>
在 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(); ?>
现在的问题是我必须在所有帖子中上传 10 张图片。例如,如果有一天我只上传了 8 张图片,我会看到 8 张图片,但我也会看到两个空格,因为我没有使用其他图片字段。
在尝试了几天后,我发现我有两种选择:
1) 使用高级自定义字段插件的转发器字段。
2) 使用此代码:
<?php if(get_field("image") != ""): ?>
<div class="mySlides"><?php the_field("image"); ?></div>
<?php endif; ?>
中继器字段是一项高级功能。我不想为插件付费,我更喜欢自己学习如何制作,所以我决定使用第二个选项。
现在的问题是第二个选项不能正常工作。这不是我想要的:当我使用少于 10 张图片时制作没有空格的幻灯片。
我知道这段代码非常接近我想要实现的目标。你有什么建议吗?
【问题讨论】:
标签: javascript php html wordpress slideshow