zch0071 發表於 2013-12-22 15:35
4 r' |- l, q& i( H3 N1 A! m! ?$ s給一些常用代碼來看看
5 n/ X: h+ n9 E0 p9 W1、在主題文件夾中建thumbnail.php 文件,添加如下代碼: <div class="thumbnail_t"> <?php if ( get_post_meta($post->ID, 'thumbnail', true) ) : ?> <?php $image = get_post_meta($post->ID, 'thumbnail',true); ?>
+ J3 X' c- Q/ H. q+ Q<a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>"><img src="<?php echo $image; ?>" alt="<?php the_title(); ?>"/></a> <?php else: ?>
$ t) [6 ] Y$ W( F$ b; D x</div> <!-- 截圖 -->
: |& J& G. q6 L. l8 A<div class="thumbnail"> # I/ u# a7 n' U1 S4 A# F
<a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>"> ( T. Y* ]8 h9 \& }! _8 z
<?php if (has_post_thumbnail()) { the_post_thumbnail('thumbnail'); }
+ _( V" [) g9 c/ A$ Helse { ?>
( i% d# M6 m" f7 r7 a5 {' @( o <img class="home-thumb" src="<?php echo catch_first_image() ?>" width="140px" height="100px" (此處可以修改尺寸)alt="<?php the_title(); ?>"/> 6 h! j* d, x! ^" X; ]. K6 w
<?php } ?>* M6 {% a6 l" s/ E2 ]* L# P
</a> <?php endif; ?> </div>1 T* B" N2 v" y4 M. E u
2、在主題文件夾裡面的functions.php文件中添加下面代碼:沒有請新建,5 z1 p( \. \% E: M
<?php
3 m- m& I1 K/ u/ q; [+ F9 q//支持外鏈縮略圖 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; }/ G4 z1 ~4 b N: d* h
?>- R* \' n _2 J5 z
通過下面代碼調用。 <?php include('/thumbnail.php'); ?>% ~2 Q$ @; t2 v* z3 }1 a, L) ?
. P4 [8 l( z/ p- c& V5 ?$ O
5 O5 `, P2 w- W8 O
0 s$ e5 z2 L( U( a7 F( V5 L
- }" u% c/ W [( [6 ~' `8 c5 \
% N8 z" s$ F8 y. g, G" ^* k |