通过Wordpress中的数组循环

时间:2012-10-16 03:55:09

标签: php wordpress image-gallery

我创建了一个带图片库上传的自定义帖子类型。现在我试图在前端显示画廊。这是我到目前为止用于显示1个图像的内容,但如果上传了多个图像,则所有网址都会卡在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">

1 个答案:

答案 0 :(得分: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).'">';
  }
}
?>

编辑:显然,图库上传存储为逗号分隔值。将上面的代码段更新为希望帐户。