特色图像、缩略图是 WordPress 中一个不算复杂却十分难以理解的一个概念(至少我是这么认为的),特色图像的设置和调用是分两步来进行的,特色图像在 WordPress 中是属于日志缩略图的一种,即可以理解为默认缩略图的种类可分为 大、中、小和特色图像等缩略图,在后台默认的编辑器中,我们可以在文章中任意的调用大、中、小、等缩略图插入到文章,当然如果你愿意的话 WP 还可以插入原图。当主题的结构中支持将文章中的特色图像显示在文章概要或是文章索引中的时候,我们还可以在后台将喜欢的图像设置为特色图像。
可以用 the_post_thumbnail 函数 也可以用 get_the_post_thumbnail 函数,这样会直接输出带img的完整代码,有的时候并不是我们需要的,如果单纯的只需要图像的URL,我们可以这样做。
调用缩略图的URL:
- <a href="<?php the_post_thumbnail_url( 'full' ); ?>" rel="external nofollow" ><?php the_post_thumbnail(); ?></a>
如果不加链接:
- <?php if ( has_post_thumbnail() ) { ?>
- <?php the_post_thumbnail(); ?>
- <?php } else {?>
- <img src="这里填写默认图片地址"/>
- <?php } ?>
参数设置:
- 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,自定义长宽