zch0071 發表於 2013-12-22 15:35 
; Z8 K6 y# l- {: B4 Q' J給一些常用代碼來看看
% u. R' \6 K: `* n+ }1、在主題文件夾中建thumbnail.php 文件,添加如下代碼: <div class="thumbnail_t"> <?php if ( get_post_meta($post->ID, 'thumbnail', true) ) : ?> <?php $image = get_post_meta($post->ID, 'thumbnail',true); ?> # Z7 \+ _: A0 ?
<a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>"><img src="<?php echo $image; ?>" alt="<?php the_title(); ?>"/></a> <?php else: ?>
: \6 _1 R& f8 J0 [5 X9 M1 d2 W</div> <!-- 截圖 --> % H7 E, p7 ^2 g6 w4 q
<div class="thumbnail">
$ L& q5 f/ K& |- q, T<a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>"> 2 @; Y, i* |/ n+ z
<?php if (has_post_thumbnail()) { the_post_thumbnail('thumbnail'); }
0 j; F" |5 f6 @3 j' D/ `else { ?>
0 g! f5 ~4 P3 q* W; P: C/ w. f/ ^ <img class="home-thumb" src="<?php echo catch_first_image() ?>" width="140px" height="100px" (此處可以修改尺寸)alt="<?php the_title(); ?>"/>
' N0 K4 P, ?, _# i- w<?php } ?>
7 }' a9 }1 y X/ r8 @* u</a> <?php endif; ?> </div>
" e/ m# |) P/ W& `4 I7 j1 Q2、在主題文件夾裡面的functions.php文件中添加下面代碼:沒有請新建,
; a$ {% O$ f) v+ O1 t7 ?6 S. U <?php1 f" `6 r- I0 u3 }9 O& O1 J
//支持外鏈縮略圖 if ( function_exists('add_theme_support') ) add_theme_support('post-thumbnails'); function catch_first_image() { global $post, $posts; $first_img = ''; ob_start(); ob_end_clean(); $output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches); $first_img = $matches [1] [0];if(empty($first_img)){ //Defines a default image $random = mt_rand(1, 10); echo get_bloginfo ( 'stylesheet_directory' ); echo '/images/random/'.$random.'.jpg'; } return $first_img; }
C/ C+ F" ?/ G7 X?>
. S& L2 P/ S) B2 ]# X/ [& p通過下面代碼調用。 <?php include('/thumbnail.php'); ?>
6 o/ F8 S7 h! R! c
# O O1 t8 B2 M, b3 q9 ~6 d" ^5 T+ e' N8 d; U
/ ^1 H1 y' P1 t, }6 m) z) { Y1 F$ \) G% e' V( W6 s3 Z
" S/ I$ C7 E+ y2 e# T |