启用WordPress 缩略图

WordPress 2.9已经内置了缩略图的功能,但是却在后台编辑页面没有直接启用。那如何在WordPress 2.9后台启用缩略图的功能呢?方法很简单

,就是在你所用的主题的functions.php文件中添加:

if ( function_exists( ‘add_theme_support’ ) ) {
add_theme_support( ‘post-thumbnails’ );
}

添加完这个以后,你重新刷新新建文档的页面,是不是在侧边栏已经出现了缩略图的选择了。剩下的就是,如何调用文章的缩略图啦。我们以index.php为例,将index.php中下面的代码:

<div class="entry">
<?php the_content(‘Read the rest of this entry &raquo;’); ?>
</div>

修改为:

<div class="entry">
<?php if ( has_post_thumbnail() ) : ?>
<?php the_post_thumbnail( array( 125, 125 ), array( ‘class’ => ‘alignleft’ ) ); ?>
<?php endif; ?>
<?php the_content(‘Read the rest of this entry &raquo;’); ?>
</div>

其中,arry(125, 125)是说缩略图的大小,同样也可以根据后台的设置,指定为thumbnail、medium、large或者full。
至于array( ‘class’ => ‘alignleft’ ),指定的就是图片的class效果了。

进一步的,如果这篇文章没有图,那么我们怎么显示一张默认的图片(例子.jpg)呢?

<?php if ( has_post_thumbnail() ) : ?>
<?php the_post_thumbnail( array( ‘thumbnail’ ), array( ‘class’ => ‘alignleft’ ) ); ?>
<?php else : ?>
    <img src="例子.jpg" />
<?php endif; ?>

缺点就是需要手动设置每一篇文章的缩略图

小 虾

哦也,我是小虾

You may also like...