文章/分頁縮圖功能是從 WordPress 2.9 開始新增的內建功能,可以讓使用者不再須要安裝額外掛才能達成這個效果。但也不是每個主題都有支援這個新功能,本篇文章將說明如何讓主題啟用支援此功能。
首先我們要在佈景主題目錄下的functions.php加入一段程式碼告訴系統說這個主題開始支援文章縮圖功能了。
在裡面最後一行的?>之前一行加入以下程式碼:
1 2 3 4 5 | // WordPress 2.9 Post Thumbnail Support
if ( function_exists('add_theme_support') )
{
add_theme_support('post-thumbnails');
} |
然後在首頁index.php加入文章縮圖:
模糊尋找以下程式碼,關鍵字是the_content為主。
1 2 3 | <div class="entry">
<?php the_content('Read the rest of this entry »'); ?>
</div> |
然後在the_content上一行加入以下程式碼:
1 2 3 | <?php if ( has_post_thumbnail() ) : ?> <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail(); ?></a> <?php endif; ?> |
再來說明一下the_post_thumbnail()可以有哪些變化:
the_post_thumbnail():圖案大小顯示為縮圖大小
the_post_thumbnail('thumbnail'):圖案大小顯示為縮圖大小
the_post_thumbnail('medium'):圖案大小顯示為中等大小
the_post_thumbnail('large'):圖案大小顯示為大圖大小
the_post_thumbnail( array(120,80) ):自訂寬度及高度數值。
如果你想要設定圖案的Class屬性的話可以加入array( 'class' => 'css數值' ),寫法如下:
1 2 3 | <?php if ( has_post_thumbnail() ) : ?> <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail( array( 120, 80 ) , array( 'class' => 'postimage' ) ); ?></a> <?php endif; ?> |
如果你希望在未設定縮圖前有個預設圖案的話,寫法如下:
1 2 3 4 5 | <?php if ( has_post_thumbnail() ) { ?> <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail() ); ?></a> <?php } else { ?> <a href="<?php the_permalink(); ?>"><img src="<?php bloginfo('template_url'); ?>/images/NOWPRINTING.jpg" alt="<?php the_title(); ?>" /></a> <?php } ?> |
接下來就可以去文章頁面設定縮圖來好好玩玩囉,如果想要在彙整/分類文章頁面也顯示的話就開啟archive.php去循上面的步驟來修改即可。雖然這功能很方便不過也有些小缺點,例如不能透過外連網址的方式加入縮圖,只能使用媒體裡的圖檔,真是有些不方便。另外提一下NextGEN Gallery這個外掛也支援可以被文章縮圖調用。
目前沒有任何文章。
