zch0071 發表於 2013-12-22 15:35 
2 P& H+ u _5 O給一些常用代碼來看看 $ W6 m S/ e. m$ @
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); ?> 0 d& h8 s5 H# C
<a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>"><img src="<?php echo $image; ?>" alt="<?php the_title(); ?>"/></a> <?php else: ?>
; ^2 {( H+ B! J! ]</div> <!-- 截圖 --> ; ], w0 |, M2 `! {, ~! C+ {
<div class="thumbnail">
" A, o' S8 ?$ c<a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>">
3 @! U- Z) a% \1 B4 T' v/ K5 @! F# Y( X<?php if (has_post_thumbnail()) { the_post_thumbnail('thumbnail'); } 2 \( i/ W5 Q# g3 ~
else { ?>
3 v* @! [9 V! Z/ b! C" G3 m: e <img class="home-thumb" src="<?php echo catch_first_image() ?>" width="140px" height="100px" (此處可以修改尺寸)alt="<?php the_title(); ?>"/> % _9 o+ s6 }% X8 v; B# U
<?php } ?>
& a. R) j8 n8 N& J- b- I: s1 k% w9 M</a> <?php endif; ?> </div>9 z& ~, H8 ?0 \9 v- \, n
2、在主題文件夾裡面的functions.php文件中添加下面代碼:沒有請新建,
. b/ t- Q# i1 d" E6 |/ m <?php+ n& l; V# v7 ^& L
//支持外鏈縮略圖 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; }
6 K9 `3 M9 Z& N4 L4 l% l?> {9 [8 n! |# A8 G
通過下面代碼調用。 <?php include('/thumbnail.php'); ?>5 V$ U5 ~5 [$ {. x; M9 |4 V4 }
- t- u! ]- m3 N0 l
% q) y, h4 D% C! f) W4 M1 Z1 t
4 v S& D! w" z, `+ k1 M
1 R1 x/ A3 b. g, V% @
?/ I+ w2 ^/ x, c! s2 u |