要在 WordPress 裡取得文章的圖片(feature images)可以使用 get_the_post_thumbnail(),不過這個方法會帶 html 的標籤,如果要直接取得圖片的網址的話,則可以使用 get_the_post_thumbnail_url()。

可以丟入的參數如下:

get_the_post_thumbnail_url( int|WP_Post $post = null, string|int[] $size = 'post-thumbnail' )

第一個值為 post 的 id,如果已經有用 the_post() 取得文章資訊,就可以不用帶參數,而第二個值則為圖片的尺寸。

範例:

if ( have_posts() ) {
    while ( have_posts() ) {
        the_post();
        $featured_img_url = get_the_post_thumbnail_url(get_the_ID(),'full'); 
    endwhile; 
endif;

 

 

參考:

get_the_post_thumbnail_url()