アイキャッチ画像を設定することで
記事一覧などで表示させたい画像のサムネイルを選べるのはとても便利な機能なのですが・・・
クライアントはそこまで求めておらず(アイキャッチ画像を設定するという手間が増えますからね)
でもデザイン的にサムネイルを表示させたい場合ってあると思います。
そんなときによく使う手がこれ。
<?php
$files = get_children("post_parent=$id&post_type=attachment&post_mime_type=image");
if ($files) :
$keys = array_keys($files);
$num = $keys[0];
$thumb = wp_get_attachment_thumb_url($num);
print '<a href="' . clean_url(get_permalink()) . '" title="' . the_title_attribute('echo=0') . '"><img src="' . clean_url($thumb) . '" width="150" height="150" alt="' . the_title_attribute('echo=0') . '" /></a>';
else :
print '<a href="' . clean_url(get_permalink()) . '" title="' . the_title_attribute('echo=0') . '"><img src="' . get_bloginfo('template_url') . '/images/no_thumbnail.jpg" alt="' . the_title_attribute('echo=0') . '" /></a>';
endif;
?>
ループ内で使います。
これで記事内の画像をサムネイルとして利用することができるようになります。
表示させたい画像を記事ごとに選ぶことはできませんが
どれでもいいから記事内の画像をサムネイルとして使いたい!
という場合におすすめです。






