【问题标题】:Looping through an array in Wordpress在 Wordpress 中循环遍历数组
【发布时间】:2012-10-16 03:55:08
【问题描述】:

我创建了一个带有图片库上传的自定义帖子类型。现在我正在尝试在前端显示画廊。到目前为止,这就是我可以显示 1 张图片的内容,但是如果上传了多张图片,所有 url 都会卡在 src 标签中。所以我猜我应该遍历那个数组并分别吐出每个数组?那会是要走的路吗?如果是这样,我该怎么做?任何帮助表示赞赏。

<?php if (have_posts()) :  while (have_posts()) : the_post(); ?>

<?php
echo '<img src="'.get_post_meta($post->ID, 'gallery-upload', true).'">';    
?>

<?php endwhile; else: ?>
    <p><?php _e('No posts were found. Sorry!'); ?></p>
<?php endif; ?>

编辑:

这是返回的内容:

<img src="http%3A%2F%2Flocalhost%3A8888%2Fandreasmoulis%2Fwp-content%2Fuploads%2F2012%2F10%2F800x400-volbeat-mock1.jpeg%2Chttp%3A%2F%2Flocalhost%3A8888%2Fandreasmoulis%2Fwp-content%2Fuploads%2F2012%2F10%2F1574_2_1.jpeg%2Chttp%3A%2F%2Flocalhost%3A8888%2Fandreasmoulis%2Fwp-content%2Fuploads%2F2012%2F10%2F1576_2_1.jpeg%2Chttp%3A%2F%2Flocalhost%3A8888%2Fandreasmoulis%2Fwp-content%2Fuploads%2F2012%2F10%2F1576_4_1.jpeg%2Chttp%3A%2F%2Flocalhost%3A8888%2Fandreasmoulis%2Fwp-content%2Fuploads%2F2012%2F10%2F2244_2_1.jpeg%2Chttp%3A%2F%2Flocalhost%3A8888%2Fandreasmoulis%2Fwp-content%2Fuploads%2F2012%2F10%2F300789_2349086884438_1168050047_32154880_1451576942_n.jpeg%2Chttp%3A%2F%2Flocalhost%3A8888%2Fandreasmoulis%2Fwp-content%2Fuploads%2F2012%2F10%2F373795_278881222158106_278880528824842_834930_1454244548_n.jpeg%2Chttp%3A%2F%2Flocalhost%3A8888%2Fandreasmoulis%2Fwp-content%2Fuploads%2F2012%2F10%2F20110909-121141.jpeg">

【问题讨论】:

    标签: php wordpress image-gallery


    【解决方案1】:

    查看function reference,get_post_meta 通常返回一个数组,除非第三个参数设置为true。这样的事情应该或多或少起作用。

    <?php
    foreach(get_post_meta($post->ID, 'gallery-upload') as $meta) {
      foreach(explode(',', $meta) as $src) {
        echo '<img src="'.htmlentities($src).'">';
      }
    }
    ?>
    

    编辑:显然画廊上传存储为逗号分隔值。将我上面的 sn-p 更新为 希望 帐户。

    【讨论】:

    • 我会玩弄它谢谢。查看我的编辑,这就是我从您的代码中得到的。任何想法为什么?
    • 嘿,哎呀。删除 rawurlencode(...) 函数(见我上面的更新),它应该修复它。
    • 仍然没有,它在一个 中返回所有内容,因此它正在生成所有链接。只是不在单独的图像标签中。
    • 啊!所以看看它返回的 $src ,它似乎给了你一个逗号分隔的值列表。看看我更新的 sn-p。
    • 你真是太棒了,先生!我很感激帮助。忘了爆炸!
    猜你喜欢
    • 1970-01-01
    • 2013-12-25
    • 2014-06-04
    • 1970-01-01
    • 2019-07-10
    • 2012-05-24
    • 2016-05-25
    相关资源
    最近更新 更多