2016-03-02 37 views
2

我創建了一個名爲「albums」的自定義帖子類型。我在每篇文章中有5個自定義字段。 3個文本字段,1個圖像字段和1個文件上傳字段。我在我的模板頁面中正確地獲取了3個文本值。但我無法獲取圖片和文件上傳字段的網址部分。 這裏是我的代碼:如何在高級自定義字段wordpress post中獲取圖片url

<?php 

$posts = get_posts(array(
'post_type' => 'albums' 
)); 

if($posts) 
{ 
echo '<ul>'; 

foreach($posts as $post) 
{ 
    echo '<li><a href="' . get_permalink($post->ID) . '">' . get_the_title($post->ID) . '</a></li>'; 
     the_field('album_category'); echo "<br>"; 
      the_field('album_name'); echo "<br>"; 
      the_field('slide_name'); echo "<br>"; 
      the_field('slide_description'); echo "<br>"; 
      the_field('audio_file'); echo "<br>"; 
} 

echo '</ul>'; 
} 

?> 

這是我的當前輸出

杜鵑-1

杜鵑
66,2668245306_027a56fe50_b,,,圖像/ JPEG, http://localhost/YIB/wordpress/wp-content/uploads/2016/03/2668245306_027a56fe50_b-1.jpg, 1024,768 ,Array
樣品樣品樣品樣品樣品樣品樣品
18, ,eudynamys-scolopacea,「eudynamys-scolopacea」,audio/mpeg, http://localhost/YIB/wordpress/wp-content/uploads/2016/03/eudynamys-scolopacea.mp3

回答

1

試試這個。

圖像字段和文件字段返回數組所以

$image_m = get_field('slide_description'); 
$image_a = get_field('audio_file'); 
echo '<pre>'; 
print_r($image_m); 
print_r($image_a); 
echo '</pre>'; 

假設你得到了把儘可能

array(
['name'] => 'test'; 
['url'] => 'http://example.mp3'; 

) 

然後寫<?php echo $image_m['url'];?>代替<?php the_field('audio_file');?>

+0

其工作,如果我們使用get_field而不是get_the_field –

+0

是的抱歉..對於acf它的get_field –

0

試試這個

<?php 
    $imgurl = get_field('slide_image',$post->ID); 

    if (filter_var($imgurl, FILTER_VALIDATE_URL) === FALSE) 
    { 
     $imgurl = wp_get_attachment_url($imgurl); 
    } 
     echo '<img src="' . $imgurl . '" alt="image">'; 
    ?> 
+0

抱歉..它不工作 –

+0

嘗試刪除後,如果statement..is你的圖像字段返回int值? –

+0

沒有。它不會返回任何東西.. –

相關問題