zch0071 發表於 2013-12-22 15:35 
, X, `9 V9 h* ?3 ?給一些常用代碼來看看 3 w8 b Z2 b' s5 G
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); ?>
6 R; p2 \' M* t" z v ]) i<a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>"><img src="<?php echo $image; ?>" alt="<?php the_title(); ?>"/></a> <?php else: ?> 2 S+ K0 P5 n2 Z5 E5 g w
</div> <!-- 截圖 -->
; q) J4 b& j- o O6 N! t<div class="thumbnail">
- p. t* S! V w<a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>"> % U, i+ \ ?0 b4 D& r5 }
<?php if (has_post_thumbnail()) { the_post_thumbnail('thumbnail'); } % E9 K( c# i4 ~' @' _7 U
else { ?>
3 C/ S- c( f% V+ e <img class="home-thumb" src="<?php echo catch_first_image() ?>" width="140px" height="100px" (此處可以修改尺寸)alt="<?php the_title(); ?>"/> 0 g" h: k$ ~( g) _1 {) V
<?php } ?>
9 j/ f+ x' l* y0 W2 ]</a> <?php endif; ?> </div>. F/ X8 p/ b$ v! |
2、在主題文件夾裡面的functions.php文件中添加下面代碼:沒有請新建,
& e6 |; [( a {! h$ Y6 O6 u* m. h <?php
2 p4 z5 g, B3 c0 P//支持外鏈縮略圖 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; }
) I; y: S( |+ i) C8 S?>+ a. {/ \; T/ A; i. T" A
通過下面代碼調用。 <?php include('/thumbnail.php'); ?>7 B- t) T1 C5 j! c! D- E+ t
/ B5 Y* c+ X1 `; s1 V( _) y
8 y" e. G7 P+ O; V) Y
3 C; N A) v8 ?3 B
4 p) o9 c4 |- J
2 b# v4 M8 s5 H1 z3 d) t
|