wordpress缩略图完美方案(自动添加特色图像)

wordpress缩略图完美方案:wordpress特色图像能够为每篇文章设置一个缩略图,但必须是手动设置,下面咱们经过代码实现自动把文章中的第一张图片添加为缩略图,若是文章中没有图片,咱们能够调用媒体库中的某个图片做为文章的缩略图,或者咱们设定一个文件夹里面上传咱们须要设定为缩略图的图片。

wordpress缩略图完美方案
wordpress缩略图完美方案

在当前主题的functions.php里添加如下代码

    function wpforce_featured() {

global $post;

$already_has_thumb = has_post_thumbnail($post->ID);

if (!$already_has_thumb)  {

$attached_image = get_children( “post_parent=$post->ID&post_type=attachment&post_mime_type=image&numberposts=1” );

if ($attached_image) {

foreach ($attached_image as $attachment_id => $attachment) {

set_post_thumbnail($post->ID, $attachment_id);

}

}

}

}  //end function

add_action(‘the_post’, ‘wpforce_featured’);

add_action(‘save_post’, ‘wpforce_featured’);

add_action(‘draft_to_publish’, ‘wpforce_featured’);

add_action(‘new_to_publish’, ‘wpforce_featured’);

add_action(‘pending_to_publish’, ‘wpforce_featured’);

add_action(‘future_to_publish’, ‘wpforce_featured’);

 

这里有一段很实用的代码,能够自动将文章中的第一张图片设置为特点图像,若是你手动设置了特点图像,能够覆盖这段代码,这样即便你忘记了设置,wp会自动调用文章中的第一张图片做为缩略图。html

自动调用媒体库中的图片做为缩略图

问题来了,若是咱们的文章里没有图片,又忘了设置特点图像呢,那么咱们可让WP调用媒体库里的某张指定的图片做为缩略图。浏览器

    function wpforce_featured() {

global $post;

$already_has_thumb = has_post_thumbnail($post->ID);

if (!$already_has_thumb)  {

$attached_image = get_children( “post_parent=$post->ID&post_type=attachment&post_mime_type=image&numberposts=1” );

if ($attached_image) {

foreach ($attached_image as $attachment_id => $attachment) {

set_post_thumbnail($post->ID, $attachment_id);

}

} else {

set_post_thumbnail($post->ID, ’66’);

}

}

}  //end function

add_action(‘the_post’, ‘wpforce_featured’);

add_action(‘save_post’, ‘wpforce_featured’);

add_action(‘draft_to_publish’, ‘wpforce_featured’);

add_action(‘new_to_publish’, ‘wpforce_featured’);

add_action(‘pending_to_publish’, ‘wpforce_featured’);

add_action(‘future_to_publish’, ‘wpforce_featured’);

 

其中

$post->ID

, ‘

66

‘是媒体库中某张图片的id
如何查看媒体库里某张图片的ID呢?
WP-后台-多媒体-媒体库,采用一个列表浏览方式,把鼠标指向图片,在浏览器的下面会显示图片的ID,或者你阅读下面这篇文章来实现:
wordpress后台无插件显示文章和分类ID服务器

自定义图片做为特点图像

    /**

* 添加特点缩略图支持相关文章带有缩略图

*/

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

//让你的主题开启特点图像功能

function post_thumbnail_src(){

global $post;

if( $values = get_post_custom_values(“thumb”) ) {//输出自定义域图片地址

$values = get_post_custom_values(“thumb”);

$post_thumbnail_src = $values [0];

} elseif( has_post_thumbnail() ){//若是有特点缩略图,则输出缩略图地址

$thumbnail_src = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID),’thumbnail’);

//其中full能够修改的,能够为thumbnail, medium, large or full(分别表明最小的缩略图、中等、大和原始尺寸)

$post_thumbnail_src = $thumbnail_src [0];

} else {

$post_thumbnail_src = ”;

ob_start();

ob_end_clean();

$output = preg_match_all(‘/<img.+src=[‘”]([^'”]+)[‘”].*>/i’, $post->post_content, $matches);

$post_thumbnail_src = $matches [1] [0];   //获取该图片 src

if(empty($post_thumbnail_src)){ //若是日志中没有图片,则显示随机图片

$random = mt_rand(1, 10);

echo get_bloginfo(‘template_url’);

echo ‘/images/pic/’.$random.’.jpg’;

//若是日志中没有图片,则显示默认图片

//echo ‘/images/default_thumb.jpg’;

}

};

echo $post_thumbnail_src;

}

 

咱们在主题下创建图像文件夹

/images/pic/

,里面上传你先显示的图片,做为随机调用,而

/images/default_thumb.jpg

最为默认调用,固然这两个功能咱们选择一个就OK了,因此注释掉了一个。dom

PS:调用特点图像和设置大小

要让你的主题支持特点图像必须在functions.php中加入如下代码:wordpress

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

 

在post模板中调用:post

    <?php

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

the_post_thumbnail();

}

?>

<?php the_content(); ?>

 

能够调用不一样尺寸的图片:url

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

the_post_thumbnail(‘thumbnail’);// Thumbnail(默认尺寸 150px150px max)

the_post_thumbnail(‘medium’);   // Medium resolution(default300px300px max)

the_post_thumbnail(‘large’);    // Large resolution(default640px640px max)

the_post_thumbnail(‘full’);     // Full resolution(original size uploaded)

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

 

总结

一、你若是再制做本身的主题,在调试中,能够尝试一个你喜欢的方法。
二、你的站点已经发表了不少文章,而且都启用发特点图像,

要慎用,可能你选择的某一个代码功能会影响你已经发布的文章不能正常显示!

插件

三、特点图像会占用大量的服务器空间,由于每张图片都会裁剪成多张大小不一样的缩略图方便在不一样的位置调用,最主要的是不支持外链。调试

本文《wordpress缩略图完美方案(自动添加特色图像)》由网赚联盟( wangzhuan.org.cn )整理或原创,感谢您的阅读。

随机文章

SEO小小课堂网
SEO教程
站长导航
搜素引擎算法
关键词排名优化
网站内容优化
SEO小小课堂网
SEO教程

百度搜索“网赚联盟”即可找到本站,微信搜索“小小课堂网”关注小小课堂网公众号。网赚联盟( wangzhuan.org.cn )欢迎用户投稿,发布者:用户投稿,文章版权归作者所有,投稿文章不代表网赚联盟立场,中二少年发布为网赚联盟原创文章,转载请注明出处:https://wangzhuan.org.cn/10856.html