zch0071 發表於 2013-12-22 15:35 2 J$ F% f% G+ ~, n
給一些常用代碼來看看
) P+ {& g. S0 n1、在主題文件夾中建thumbnail.php 文件,添加如下代碼: <div class="thumbnail_t"> <?php if ( get_post_meta($post->ID, 'thumbnail', true) ) : ?> <?php $image = get_post_meta($post->ID, 'thumbnail',true); ?> : `) t) C+ N8 n5 ^, n( T* U
<a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>"><img src="<?php echo $image; ?>" alt="<?php the_title(); ?>"/></a> <?php else: ?> |* J/ H$ J) M
</div> <!-- 截圖 -->
5 I7 [% C+ [9 |) ^* w<div class="thumbnail"> 3 P$ G$ O9 ?, O! q* q; T
<a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>"> 7 u& x+ `. ~3 F6 f8 V
<?php if (has_post_thumbnail()) { the_post_thumbnail('thumbnail'); }
3 w! C' B; e6 selse { ?>
" l7 \' m3 ~3 B' X4 e7 z <img class="home-thumb" src="<?php echo catch_first_image() ?>" width="140px" height="100px" (此處可以修改尺寸)alt="<?php the_title(); ?>"/>
2 ]& c, F. }4 g3 ~& W<?php } ?> f. B% h9 Y$ I! C5 h1 P ~1 M) ^
</a> <?php endif; ?> </div>
$ L7 D/ p( n, ^" T, I2、在主題文件夾裡面的functions.php文件中添加下面代碼:沒有請新建,
( R. x7 M- T, [$ {8 _: i% l <?php8 ?( S0 l) |5 l0 _
//支持外鏈縮略圖 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; }2 a) P* y, k6 n$ _+ `2 M( \- @# |1 {
?>% c% @ F& \* i* ^+ n
通過下面代碼調用。 <?php include('/thumbnail.php'); ?>
! x# G3 F% X8 ^ l3 W
: V" c* _8 u2 }7 v2 c& b; r/ y9 j" K. H: B2 s9 P+ W
4 x( _! X: T& Y2 P) y" e
* V7 N# B% r! _# i9 y/ g
4 C! T8 t+ A/ V* e z; k |