2012-03-30 77 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,得到所有)