2014-12-05 72 views
0

我正在为'垃圾'中的某些帖子生成查询。我可以检索精选图片网址,但我正在寻找一种方法来获取服务器上的精选图片文件位置。WordPress精选图片的实际文件位置?

# generate the query 
foreach ($query as $post) { 
    $thumbID = get_post_thumbnail_id($post->ID); 
    $thumbObj = get_post($thumbID); 
    $source_url = $thumbObj->guid; 
    # how do I get the string "/path/to/wordpress/media/library/thumbnail.jpg" ? 
} 

我无法找到任何返回实际特色图像文件位置的WordPress函数。

+1

你的意思是这样的:http://codex.wordpress.org/Function_Reference/wp_get_attachment_image_src wp_get_attachment_image_src – 2014-12-05 00:42:09

回答

2

没有wp功能,但它很容易使你自己,例如

function return_image_path($thumbID) { 

    $image= wp_get_attachment_image_src($thumbID); 
    $imagepath= str_replace(get_site_url(), $_SERVER['DOCUMENT_ROOT'], $image[0]); 

    if($imagepath) return $imagepath; 

    return false; 

} 
+0

非常接近,尽管代码假设WordPress安装在根目录中。但是它的确让我找到了一个非常类似的解决方案wp_upload_dir()函数。 – johnh10 2014-12-05 15:33:23

1

扩大在大卫的回答,这个工作对我来说:

function return_image_path($thumbID) { 
        $thumbID = get_post_thumbnail_id($post->ID); 
        $image= wp_get_attachment_image_src($thumbID); 
        $upload_dir = wp_upload_dir(); 
        $base_dir = $upload_dir['basedir']; 
        $base_url = $upload_dir['baseurl']; 
        $imagepath= str_replace($base_url, $base_dir, $image[0]); 
        if (file_exists($imagepath)) return $imagepath; 
        return false; 
} 
0
$post_thumbnail_id = get_post_thumbnail_id($post->ID); 
var_dump(get_attached_file($post_thumbnail_id));