2012-10-16 141 views
0

我創建了一個自定義的帖子類型,其中包含一個圖片庫上傳。現在我正試圖在前端顯示畫廊。這是我迄今爲止能夠顯示1張圖片的功能,但是如果上傳多張圖片,所有網址都會卡在src標籤中。所以我猜我應該循環遍歷這個數組並且分別吐出每一個?那是否會成爲一條路呢?如果是的話,我該如何做到這一點?任何幫助表示讚賞。在Wordpress中循環訪問數組

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

望着function reference,除非第三個參數設置爲true get_post_meta通常返回一個數組。像這樣的東西應該可以工作,或多或少。

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

編輯:顯然畫廊上傳存儲爲逗號分隔值。更新我的片段上面有希望帳戶爲此。

+0

我會玩它,謝謝。看看我的編輯,這就是我的代碼。任何想法爲什麼? – souporserious

+0

嘿,哎呀。刪除rawurlencode(...)函數(請參閱上面的更新),它應該修復它。 – adamdunson

+0

仍然沒有任何東西,它將所有內容都返回因此,它會生成所有鏈接。只是不在單獨的圖像標籤。 – souporserious