如何设置WordPress文章特色图像(Featured Image)

369次阅读
没有评论
内容纲要

在主题的functions.php中添加如下代码

1

2

3

4

//使WordPress支持post thumbnail

if ( function_exists( 'add_theme_support' ) ) {

    add_theme_support( 'post-thumbnails' );

}

注意:这段代码应当加载functions.php的body中,不要写进函数里。

1

add_image_size( $name, $width, $height, $crop );

在functions.php中,写在add_theme_support()之后,完整代码如下

1

2

3

4

5

6

7

8

//add post thumbnails

if ( function_exists( 'add_theme_support' ) ) {

    add_theme_support( 'post-thumbnails' );

}

if ( function_exists( 'add_image_size' ) ) {

    add_image_size( 'customized-post-thumb', 100, 120 );

}

创建几个不同的缩略图尺寸,用到的函数:

Post Thumbnail功能详细说明

如何调用特色图像

在post模板中

1

2

3

4

5

6

<?php

if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it.

  the_post_thumbnail();

}

?>

<?php the_content(); ?>

可以调用不同尺寸的图片

1

2

3

4

5

6

7

8

the_post_thumbnail();                  // 无参数,默认调用Thumbnail

the_post_thumbnail('thumbnail');       // Thumbnail (默认尺寸 150px x 150px max)

the_post_thumbnail('medium');          // Medium resolution (default 300px x 300px max)

the_post_thumbnail('large');           // Large resolution (default 640px x 640px max)

the_post_thumbnail('full');            // Full resolution (original size uploaded)

the_post_thumbnail( array(100,100) );  // Other resolutions

如何从后台修改缩略图尺寸

如何设置WordPress文章特色图像(Featured Image)

从后台修改缩略图尺寸

访问后台>>设置>>媒体,缩略图大小这一项就是特色图像(Featured Image or Thumbnail)的尺寸,也就是the_post_thumbnail()不加参数时调用的图片的尺寸。根据需要修改其参数即可。上传图片时WordPress会自定生成这个尺寸的图片。

为文章添加特色图片的三种方法

编辑文章时我们有三种方式添加特色

  1. 上传图片时点击“作为特色图像”进行设置,如下图所示,点击后显示“完成”即表示设置成功。设置好的特色图像会在右侧栏目中显示出来。
如何设置WordPress文章特色图像(Featured Image)

上传图片时设置特色图片

  1. 点击右侧栏目中的特色图像设置,如下图所示,点击“设置特色图像”按钮后弹出与方法一一样的界面,设置方法也相同
如何设置WordPress文章特色图像(Featured Image)

从右侧工具栏设置特色图像

  1. 如果你没有用上述两种方法设置,那么你也许希望从文章中已经存在的图片中选取一张作为特色图像,WordPress考虑的很周到,你可以轻松选择文中已有的图像。

点击右侧工具栏的设置特色图像按钮,弹出如下所示对话框,选项卡切换到相册,就可以看到所有文中已经插入的图片,点击显示就会出现和方法一一样的界面,照着方法一设置即可。

如何设置WordPress文章特色图像(Featured Image)

选项卡切换到“相册”

正文完
 
周 杰
版权声明:本站原创文章,由 周 杰 2016-06-17发表,共计1850字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
评论(没有评论)