2013-12-16 31 views
2

我一直在開發一個wordpress主題,並且我注意到很多wordpress函數有兩個版本給他們正常的,比如the_post_thumbnail和get_在前面的同一個它。wordpress中的_post_thumbnail和get_the_post_thumbnail之間的區別

<?php the_post_thumbnail(); ?> 
<?php get_the_post_thumbnail(); ?> 

這不只是爲了the_post_thumbnail,因爲我看到了很多不同的WordPress的功能,想知道有什麼區別兩者,因爲他們似乎做同樣的事情,要確保我正確使用它們。謝謝。

回答

5

the_post_thumbnail()僅適用於郵編,並返回當前帖子的精選圖片。 get_the_post_thumbnail()無處不在,並獲得$ post_id參數在第一個屬性。

篩選〜:

the_post_thumbnail($params) = get_the_post_thumbnail($current_post_id,$params); 

這是實現在WP發動機:

function the_post_thumbnail ($size='post-thumbnail', $attr=''){ 
    echo get_the_post_thumbnail(null, $size, $attr); 
} 

function get_post_thumbnail_id($post_id = null) { 
    $post_id = (null === $post_id) ? get_the_ID() : $post_id; 
    return get_post_meta($post_id, '_thumbnail_id', true); 
} 

如果$post_id is NULL - 電流後使用get_the_ID()

+0

好的,謝謝,所以我想給帖子以外的帖子縮略圖打個招呼,我在使用get版本的時候是正確的?如果我在循環中只使用沒有得到的版本。 – Greenhoe

+0

正確。獲取您需要的帖子ID。 – BaBL86

5

get_開頭的WordPress函數通常爲return數據,而它們的非前綴對應數據爲echo(即打印)數據。