【问题标题】:Array function to image list数组函数到图像列表
【发布时间】:2013-04-21 21:48:08
【问题描述】:

我正在使用 WordPress 和高级自定义字段,并且我想显示 3-6 张图像,具体取决于在选择字段帖子类型上选择了哪些字段。

我有它,因此值/变量与图像名称相关联,因此它会显示根据您的选择选择的图像。

例如“红色:红色”

<img src="example.com/images/image-<?php the_field(color) ?>.jpg" alt="<?php the_field(color) ?>">

Now the noob question is, when more than one field is selected, it returns an array (i.e. red, green, black.) and I can't really have the function output as an array since it won't match the图像名称。

有人知道如何为多个图像输出分离一个数组吗?

【问题讨论】:

  • 您是在问如何从数组中检索项目?
  • 是的,但我希望数组中的所有项目根据值名称分别显示为图像。到目前为止,我正在使用 foreach ($image_list as $image) { printf('', $image['gem_selection_3' ], $image['gem_selection_3']); }

标签: php


【解决方案1】:
foreach ($image_list as $image) {
    printf('<img src="example.com/images/image-%s.jpg" alt="%s" />', $image['color'], $image['color']);
}

如果您没有关联的数组,而只是一个简单的数组,您可以这样做:

foreach ($gem_colors as $color) {
    printf('<img src="example.com/images/image-%s.jpg" alt="%s" />', $color, $color);
}

【讨论】:

  • 仍在尝试解决这个问题,越来越近但出现 php 错误。这是我正在实施的网站teambauce.com/test
  • 我的自定义字段类型为“gem_selection_1”,数组显示宝石标题,例如红、蓝、绿
【解决方案2】:

类似:

<?php foreach($imageArray as $row): ?>
    <img src="example.com/images/image-<?php echo $row['color']; ?>.jpg" alt="<?php echo $row['color']; ?>">
<?php endforeach; ?>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-03-03
    • 1970-01-01
    • 1970-01-01
    • 2011-07-28
    • 2012-04-09
    • 1970-01-01
    • 2018-08-28
    • 2019-07-23
    相关资源
    最近更新 更多