2014-09-03 48 views
1

我想通了,至今:如何在wordpress中获得中等大小的发布缩略图网址?

我有这样的查询:

$categories=get_category_by_slug('my_category_slug') 
$posts_array = get_posts(array('category'=>$categories->cat_ID, 'numberposts' => -1)); 
foreach($posts_array as $post_array){ 
    $queried_post = get_post($post_array->ID); 
    //I can get the source file link this way: wp_get_attachment_url(get_post_thumbnail_id($queried_post->ID)) 
} 

但源文件实在是太大了。像the_post_thumbnail(medium)这样的函数不适用于我,因为它不仅仅是url。这是一个带有图像标签封装等的网址。那么有没有办法获得中等(或小)尺寸文件的链接?

也有可能与主题支持和后期缩略图行后设置在functions.php的帖子缩略图大小:

add_theme_support('post-thumbnails'); 
set_post_thumbnail_size(300, 300); 

我没有尝试,但我不希望设置所有缩略图的大小。

回答

10

使用wp_get_attachment_image_src(get_post_thumbnail_id($post_array->ID), 'medium')

这将返回一个包含此图片的URL,宽度,高度和裁剪模式的数组。

编辑:更新以添加全码:

$categories = get_category_by_slug('my_category_slug'); 
$posts_array = get_posts(array('category' => $categories->term_id, 'numberposts' => -1)); 
foreach($posts_array as $post_array){ 
    if(has_post_thumbnail($post_array->ID)) { 
     $image_arr = wp_get_attachment_image_src(get_post_thumbnail_id($post_array->ID), 'medium'); 
     $image_url = $image_arr[0]; // $image_url is your URL. 
    } 
} 
+0

THX但wp_get_attachment_image_src($ post_array-> ID, “中”)和wp_get_attachment_image_src($ queried_post-> ID, “中”)没” t工作 – user2718671 2014-09-03 14:22:06

+0

啊,我明白了。 $ post_array是你的贴子,而不是你的附件。我已经编辑了我的代码 - 随时尝试一下。 – 2014-09-03 14:24:07

+0

Thx,但仍然不起作用... :( – user2718671 2014-09-03 14:57:50