zch0071 发表于 2013-12-22 15:35
给一些常用代码来看看
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); ?>
<a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>"><img src="<?php echo $image; ?>" alt="<?php the_title(); ?>"/></a> <?php else: ?>
</div> <!-- 截图 -->
<div class="thumbnail">
<a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>">
<?php if (has_post_thumbnail()) { the_post_thumbnail('thumbnail'); }
else { ?>
<img class="home-thumb" src="<?php echo catch_first_image() ?>" width="140px" height="100px" (此处可以修改尺寸)alt="<?php the_title(); ?>"/>
<?php } ?>
</a> <?php endif; ?> </div>
2、在主题文件夹里面的functions.php文件中添加下面代码:没有请新建,
<?php
//支持外链缩略图 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; }
?>
通过下面代码调用。 <?php include('/thumbnail.php'); ?>
|