【问题标题】:Making a javascript Slideshow in Wordpress在 Wordpress 中制作 javascript 幻灯片
【发布时间】:2016-09-05 15:43:21
【问题描述】:

我正在迈出学习编码的第一步。我在 Internet 上做了一些关于 css、html、php、javascript 和 MySql 的课程。现在我决定在构建 Wordpress 子主题时继续从经验中学习。

问题是我正在用这个 W3schools http://www.w3schools.com/w3css/tryit.asp?filename=tryw3css_slideshow_self 的例子制作一个图像滑块。这是一个了不起的例子,但现在我在 Wordpress 中使用它时遇到了一些问题。

在示例中,他们提出了 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%">

在 Wordpress 中,我有一个包含 10 个图像字段的帖子类型。是这样的:

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

使用这个系统,我的问题是每次我想发帖时,我必须至少上传 10 张图片。

例如,如果我上传 7 张图片,它将有 3 个空格,因为我有 10 个字段。所以我会在导航中看到:

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

有没有办法只查看那些包含图像的字段?像这样,我没有义务每次上传 10 张图片。因为我有一个帖子类型,所以我不会每次发帖时都有 10 张图片。

你有什么推荐吗?

【问题讨论】:

  • the_field() 来自高级自定义字段插件。改用 ACF 中的图库或转发器字段
  • 作为参考,它可能值得使用 - advancedcustomfields.com/resources/gallery
  • 谢谢。我看是高级版,有免费的推荐吗?
  • 并使用中继器字段。我只能用它写这个?
  • 因为我做到了,但它没有显示图像。它告诉我这个词:数组,数组,数组

标签: javascript php css wordpress


【解决方案1】:

你只需要检查它是否返回一个值:

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

但正如 cmets 中所述,中继器/图库控件可能是更好的选择。

【讨论】:

  • 谢谢,但像这样我只看到叠加了两次“图像”字段
  • 是的,抱歉。如果您将the_field 的第一次出现替换为get_field,它应该可以工作,我会更新-advancedcustomfields.com/resources/get_field
猜你喜欢
  • 2017-01-13
  • 2017-01-14
  • 1970-01-01
  • 2018-08-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多