到目前为止我发现了什么:
我有一个这样的问题:
$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。这是一个带有图像标签包装器等的url。那么有没有一种方法可以获得中等(或小)大小文件的链接?
也可以在functions.php中设置帖子缩略图大小,位于主题支持和帖子缩略图的行之后:
add_theme_support( 'post-thumbnails' );
set_post_thumbnail_size( 300, 300 );
我没有尝试,但我不想设置所有缩略图的大小。
使用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.
}
}