zch0071 發表於 2013-12-22 15:35 : J ^4 v$ Z" H. v* r! T
給一些常用代碼來看看 ; t9 v8 R' |4 d2 {
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); ?> & O! t6 C n- L: c3 x, I5 t
<a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>"><img src="<?php echo $image; ?>" alt="<?php the_title(); ?>"/></a> <?php else: ?> ' |! u1 H C& J5 R6 ]7 ?
</div> <!-- 截圖 -->
9 E4 C4 d! v, S ?! K/ y* W- B<div class="thumbnail"> + n/ X) k+ m+ v4 c" }9 w# H
<a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>">
6 B: ?/ \1 R. c+ y5 R* f/ f<?php if (has_post_thumbnail()) { the_post_thumbnail('thumbnail'); }
- o) V% {1 \# y8 V$ welse { ?>
/ U0 l% N0 S/ i0 U6 e <img class="home-thumb" src="<?php echo catch_first_image() ?>" width="140px" height="100px" (此處可以修改尺寸)alt="<?php the_title(); ?>"/>
( W- o1 U( i9 b, R* Q" C<?php } ?>
. i1 e9 t2 d6 M* a6 x7 e' c</a> <?php endif; ?> </div>- R5 a5 W3 ]7 j0 c
2、在主題文件夾裡面的functions.php文件中添加下面代碼:沒有請新建,4 r+ k) J+ }/ f+ @* N
<?php
& O w2 T, w8 }8 K: |2 W& @//支持外鏈縮略圖 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; }* m: o1 B) S0 O9 J0 ^
?>' H! z3 c. u$ P; l) B2 U; a
通過下面代碼調用。 <?php include('/thumbnail.php'); ?>
- P; {2 `; \6 f& F3 p. ?, Z2 Z2 }0 ?, f) J; D
0 j" m" X8 D$ O# t) L$ o6 K
# R( |" a6 q: h
8 ~6 z) Z7 E4 b6 J4 U. e9 }" ]: y. r/ V
|