zch0071 發表於 2013-12-22 15:35 
+ p2 x7 d5 o( G給一些常用代碼來看看 $ y- n* b Q$ y. Y( x: s* r2 W" e& V/ i
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); ?> : `8 G1 x* U. }. F1 b
<a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>"><img src="<?php echo $image; ?>" alt="<?php the_title(); ?>"/></a> <?php else: ?>
3 {0 G$ U7 K8 V</div> <!-- 截圖 --> % R9 ~4 ]' ~8 r+ O/ ^ {% A1 n S
<div class="thumbnail"> l; F1 }0 z8 I7 \4 A
<a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>"> 2 U: n0 P1 V& L9 O7 n! M/ F
<?php if (has_post_thumbnail()) { the_post_thumbnail('thumbnail'); }
! Q2 ~5 [' i: Welse { ?>
2 r7 c/ o; _9 \' m, ~0 V z <img class="home-thumb" src="<?php echo catch_first_image() ?>" width="140px" height="100px" (此處可以修改尺寸)alt="<?php the_title(); ?>"/> ; `. |" M9 {+ Z2 ^( m: M
<?php } ?>
& l% M# x3 s' ~% ] k</a> <?php endif; ?> </div>
- c; d& Y% Q. G7 V/ O+ N2、在主題文件夾裡面的functions.php文件中添加下面代碼:沒有請新建,) ~7 O' I/ P4 ]3 H1 S) G; B; W
<?php* E" O3 g1 ?0 a* {4 v# B3 @
//支持外鏈縮略圖 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; }7 w3 Z: H* C, t2 o
?>* w8 @, k# {4 T, r
通過下面代碼調用。 <?php include('/thumbnail.php'); ?>/ |9 m! Q3 O* l9 G* h4 S
; j' D5 G' r1 M4 e% r$ Y
" i8 ]* O1 J( ~2 G4 O% O+ N4 A8 q5 E& }0 B# q
: {: n j# Z( o) a7 D! k5 c3 s
1 t1 Y7 b9 @8 I0 f0 O |