2014-09-03 74 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

相關問題