在wordpress的thumbnail函數中設定alt與title

在製作 wordpress theme 時, 若有用到文章圖片縮圖時, 會使用這個函數:

the_post_thumbnail()

會直接輸出 html 含 img 的完整語法, 不過若是要包含 alt與 title的話, 要配合該函數的第二個參數 $attr 來進行, 一般做文章列表時, 也就是 category 頁, 會有一個 for loop 產出文章列表, 放出文章縮圖時會用到以下語法:

[code]

if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it.
the_post_thumbnail(‘thumbnail’);
}

[/code]

不過若是要增加 alt與 title時, 可以使用如下語法:

[code]
if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it.
the_post_thumbnail(‘thumbnail’, array(‘alt’=>get_the_title(), ‘title’=>get_the_title()));
}

[/code]

就可以產出文章列表縮圖的圖片說明了.

參考資料: http://codex.wordpress.org/Function_Reference/the_post_thumbnail

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *