2012-03-30 51 views
0

我必須在wordpress中添加圖像作爲附件。目前圖片被設置爲發佈內容的一部分,但我必須在帖子中顯示附件鏈接,以便用戶可以通過附件鏈接下載該圖片。添加圖像作爲附加鏈接在帖子主題

謝謝

+0

你將永遠只有一個圖像? – 2012-03-30 11:19:28

回答

0

如果你永遠只有一個形象 - 這將是更好的將其設置爲 「特色圖片」,然後:

$image = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'your-image-size') 

然後

echo $image[0] ;is your link 

完整代碼:

<?php if (has_post_thumbnail($post->ID)): ?> 
<?php $image = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'full'); ?> 
<a href='<?php echo $image[0]; ?>'> my image link </a> 

,如果你不希望使用特色圖片 - 下一個功能將讓你總是第一個圖像中的後

// Get URL of first image in a post 

function postimage($echo = true) { 
    $image = get_children(array(
     'post_parent' => get_the_ID(), 
     'post_type' => 'attachment', 
     'numberposts' => 1, 
     'order' => 'asc', 
     'orderby' => 'ID', 
     'post_mime_type' => 'image', 
    )); 
    $image_url = ($image) ? wp_get_attachment_url(current($image)->ID) : "No Image"; 
    if($echo) 
     echo $image_url; 
    else 
     return $image_url; 
} 

,如果你有一個以上的圖像的東西就會變得稍微有點複雜 - 你將有搜索所有附件並遍歷數組。 (改變'numberposts'=> -1,得到所有)