為您的佈景主題增添「文章/插頁縮圖」

為您的佈景主題增添「文章/插頁縮圖」

官方將 the_post_image 函式更名為 the_post_thumbnail 了,所以我就乾脆重寫一次。

「文章縮圖(插頁縮圖)」是 2.9 的新功能之一,而這個功能需要佈景主題的支援。該功能的效果,就是在首頁與彙整頁面顯示文章所屬的圖片;在單篇文章頁面不會顯示該圖片(要設也是可以)。個人認為在單篇文章頁面與設定插頁縮圖好像沒什麼意義。

只要佈景主題支援,在您寫文章或插頁的頁面中,就會自動出現如右圖般的選項來讓您設定。

修改佈景主題的 functions.php 檔案

先於 functions.php 檔案中增添下述語法:

if ( function_exists( 'add_theme_support' ) ) {
	add_theme_support( 'post-thumbnails' );
}

修改佈景主題的 index.php 檔案

index.php 檔案中的:

<div class="entry">
	<?php the_content('Read the rest of this entry &raquo;'); ?>
</div>

修改為:

<div class="entry">
	<?php if ( has_post_thumbnail() ) : ?>
		<a href="<?php the_permalink(); ?>"><?php the_post_thumbnail( array( 75, 75 ), array( 'class' => 'alignleft' ) ); ?></a>
	<?php endif; ?>
	<?php the_content('Read the rest of this entry &raquo;'); ?>
</div>

原始碼中的 ( 75, 75 ) 表示圖片大小(前述的預設是 75×75 pix),'class' => 'alignleft' 表示佈景主題樣式表中的 .alignleft 樣式設定(需設定才有浮動的效果)。

如果您不想以數字來設定的話,the_post_thumbnail 有基本的四種值,分別是 thumbnailmediumlargefull;前三種值跟您的後台設定有關,最後的 full 則是圖片的原始尺寸。

例如:只要將 array( 75, 75 ) 改為 'medium' 即可。

修改佈景主題的 archive.php 檔案

archive.php 檔案中的:

<div class="entry">
	<?php the_content() ?>
</div>

修改為:

<div class="entry">
	<?php if ( has_post_thumbnail() ) : ?>
		<a href="<?php the_permalink(); ?>"><?php the_post_thumbnail( array( 75, 75 ), array( 'class' => 'alignleft' ) ); ?></a>
	<?php endif; ?>
	<?php the_content() ?>
</div>

完成之後,就可以在新增/編輯文章的頁面來選擇使用文章縮圖。

於首頁的頁面時

於彙整的頁面時

文章分類

文章標籤