投稿時にカスタムフィールドに値を入れておく事で記事内のサムネイルを表示できます。
お好きな所にサムネイル画像を表示する時に便利です。
手順1: 使用中のテーマ内のfunctions.phpを開いて下記コードを記述。
function image_attachment($key, $width, $height) {
global $post;
$custom_field = get_post_meta($post->ID, $key, true);
if($custom_field) {
echo '<img class="thumbnails_image" src="'.$custom_field.'" alt="" width="'.$width.'" height="'.$height.'" />';
}
else { //else, return
return;
}
}
手順2: 投稿時にカスタムフィールドを入力
「名前」→「image」
「値」 →「画像のパス」 (例:/wp-content/uploads/1.jpg)
手順3: 以下のコードをファイル内に記述。カスタムフィールドから画像の読み込み。
ウィジェットでサイドバーなどに表示する場合はプラグインExecutable PHP widgetが便利です。
リストでカテゴリ1のサムネイルをリンク付で3件表示する場合
カッコ内は(名前、横幅、縦幅) ※サイズをお好みで指定
<ul>
<?php query_posts('showposts=3&cat=1'); ?>
<?php while (have_posts()) : the_post(); ?>
<li>
<a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>">
<?php image_attachment('image', 200, 100); ?></a>
</li>
<?php endwhile; ?>
</ul>
HTML出力例:
<ul> <li> <a href="#" rel="bookmark" title="タイトル"> <img class="thumbnails_image" src="1.jpg" alt="" width="200" height="100" /></a> </li> <li> <a href="#" rel="bookmark" title="タイトル"> <img class="thumbnails_image" src="2.jpg" alt="" width="200" height="100" /></a> </li> <li> <a href="#" rel="bookmark" title="タイトル"> <img class="thumbnails_image" src="3.jpg" alt="" width="200" height="100" /></a> </li> </ul>