如何在wordpress中获得中等大小的缩略图网址?

时间:2014-09-03 13:58:53

标签: wordpress thumbnails

到目前为止我想到了什么:

我有这样的查询:

$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 )这样的功能对我不起作用,因为它不仅仅是网址。它是一个带有图像标签包装器等的网址。那么有没有办法获得中型(或小型)文件的链接?

还可以在主题支持和后缩略图的行之后的functions.php中设置后缩略图大小:

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

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

1 个答案:

答案 0 :(得分:14)

使用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.
    }
}