简体   繁体   English

在Wordpress中制作JavaScript幻灯片

[英]Making a javascript Slideshow in Wordpress

I am making my first steps learning to code. 我正在迈出第一步,学习编码。 I made some courses on Internet about css, html, php, javascript and MySql. 我在Internet上做了一些关于CSS,HTML,PHP,JavaScript和MySql的课程。 And now I decide to continue learning from the experience while I build a Wordpress child theme. 现在,我决定在构建Wordpress儿童主题时继续从经验中学习。

The thing is that I`m making an image slider from this example of W3schools http://www.w3schools.com/w3css/tryit.asp?filename=tryw3css_slideshow_self . 问题是,我正在从W3schools的这个示例http://www.w3schools.com/w3css/tryit.asp?filename=tryw3css_slideshow_self制作图像滑块。 It's an amazing example but now I have some problems to use it in Wordpress. 这是一个了不起的例子,但是现在我在Wordpress中使用它时遇到了一些问题。

In the example they propose 4 images like this: 在示例中,他们提出了4张这样的图像:

<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%">

In Wordpress I have a post type with 10 fields for the images. 在Wordpress中,我有一个带有10个字段的图片类型的帖子。 It's something like this: 就像这样:

<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>

Using this system, my problem is that I'm obliged to upload 10 images min each time I want to make a post. 使用此系统,我的问题是每次发表帖子时,我必须至少上传10张图片。

If I upload 7 images for example, it will have 3 empty spaces because I have 10 fields. 例如,如果我上传7张图片,由于我有10个字段,它将有3个空白。 So what I would see in my navigation would be: 因此,我将在导航中看到:

image_1 > image_2 > image_3 > image_4 > image_5 > image_6 > image_7 > image_8 (blank space) > image_9 (blank space) > image_10 (blank space) > image_1 image_1> image_2> image_3> image_4> image_5> image_6> image_7> image_8(空白) > image_9(空白) > image_10(空白) > image_1

Is there a way to only see those fields that contain an image? 有没有办法只查看包含图像的字段? Like this I'm not obliged to upload 10 images each time. 这样,我不必每次都上传10张图片。 Because I have a post type so I will not have 10 images each time I want to make a post. 因为我有一个帖子类型,所以我每次要发布时都不会有10张图像。

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

You just need to check if it returns a value: 您只需要检查它是否返回值:

<?php if(get_field("image") != ""): ?>
        <div class="mySlides"><?php the_field("image"); ?></div>
<?php endif; ?>

But as mentioned in the comments a repeater/gallery control might be a better option. 但是如评论中所述,中继器/图库控制可能是一个更好的选择。

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

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