我已经创建了一个带有图片库上传的自定义帖子类型。现在,我正在尝试在前端显示图库。这是我到目前为止可以显示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; ?>
 编辑:
这就是我最终得到的结果。。。
<?php
foreach(get_post_meta($post->ID, \'gallery-upload\') as $meta) {
 foreach(explode(\',\', $meta) as $src) {
  echo \'<img src="\'.htmlentities($src).\'">\';
 }
}
?>
 
                SO网友:fuxia
                您应该重新组织存储图像的方式:将上载的文件作为该特定帖子的子项,不要将它们放在帖子元字段中。然后使用get_children(). 查看[gallery] 一些示例的快捷代码。
我应该这样说:
$args = array( 
   \'post_mime_type\' => \'image\',
   \'numberposts\'    => -1,
   \'post_parent\'    => get_the_ID(),
   \'post_type\'      => \'attachment\' 
);
$attached_images = get_children( $args );
foreach ( $attached_images as $image )
{
    // print image
}
 而且,即使您想保留post元字段,也不要存储URL,而是使用附件ID。URL可以随时更改(请考虑从开发人员迁移到生产站点)。