zch0071 發表於 2013-12-22 15:35 3 P7 a6 G# |- L6 H0 z3 Q- R/ W
給一些常用代碼來看看
& r8 f$ L, [; l: O% j% V( H1、在主題文件夾中建thumbnail.php 文件,添加如下代碼: <div class="thumbnail_t"> <?php if ( get_post_meta($post->ID, 'thumbnail', true) ) : ?> <?php $image = get_post_meta($post->ID, 'thumbnail',true); ?> 7 E5 N4 p, s+ i( q9 ]
<a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>"><img src="<?php echo $image; ?>" alt="<?php the_title(); ?>"/></a> <?php else: ?> 3 x8 G( g% H& F1 k% f
</div> <!-- 截圖 -->
( i' A3 ^1 a% R0 j<div class="thumbnail">
4 z: r$ F/ @% B$ @<a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>"> $ z7 S+ u5 ~7 V/ g
<?php if (has_post_thumbnail()) { the_post_thumbnail('thumbnail'); }
. V0 A! p% \ T1 t( [else { ?>0 y( j7 H$ p ]
<img class="home-thumb" src="<?php echo catch_first_image() ?>" width="140px" height="100px" (此處可以修改尺寸)alt="<?php the_title(); ?>"/> " f* X7 k1 f2 z3 O3 a& L+ B
<?php } ?>0 x3 V+ h; B) _1 b0 b
</a> <?php endif; ?> </div>6 v5 D) e; Q! P
2、在主題文件夾裡面的functions.php文件中添加下面代碼:沒有請新建,
6 T2 V: O" A# `; g/ ` <?php6 l- E. ^- @" e5 F- L6 f) 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; }
, n- a# n3 J7 Y?>
# n7 p7 D0 J: S7 j通過下面代碼調用。 <?php include('/thumbnail.php'); ?>0 P+ ~3 ?/ }$ b) \" h: A4 G
/ `1 S0 \8 V6 z$ \& S5 k& O. A/ F/ r% @# S3 k' X# a4 `# Z/ o" r
* q* ?, O5 J$ Y- T ?' {
% Q/ ~/ L3 w) l3 n u% Y5 u* u3 f p% v" ~, I- ]# p. B
|