wordpress免插件自动为文章添加设置特色图像的三种方法

前端之家收集整理的这篇文章主要介绍了wordpress免插件自动为文章添加设置特色图像的三种方法前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

@H_502_0@wordpress特色图像可以为每篇文章设置一个缩略图,但必须是手动设置,下面我们通过代码实现自动文章中的第一张图片添加缩略图,如果文章中没有图片,我们可以调用媒体库中的某个图片作为文章缩略图,或者我们设定一个文件夹里面上传我们需要设定为缩略图图片

@H_502_0@

在当前主题的functions.PHP添加以下代码

@H_502_0@function wpforcefeatured() {

@H
5020@    global $post;

@H
502_0@    $already_has_thumb = has_postthumbnail($post->ID);

@H
502_0@    if (!$already_hasthumb)  {

@H
502_0@        $attached_image = get_children( "post_parent=$post->ID&post_type=attachment&post_mimetype=image&numberposts=1" );

@H
502_0@        if ($attachedimage) {

@H
502_0@                foreach ($attached_image as $attachmentid => $attachment) {

@H
502_0@                set_post_thumbnail($post->ID, $attachmentid);

@H
5020@            }

@H
5020@        }

@H
5020@    }

@H
5020@}  //end function

@H
502_0@add_action('the_post', 'wpforcefeatured');

@H
502_0@add_action('save_post', 'wpforcefeatured');

@H
502_0@add_action('draft_to_publish', 'wpforcefeatured');

@H
502_0@add_action('new_to_publish', 'wpforcefeatured');

@H
502_0@add_action('pending_to_publish', 'wpforcefeatured');

@H
502_0@add_action('future_to_publish', 'wpforcefeatured');

@H
502_0@这里有一段很实用的代码,可以自动文章中的第一张图片设置为特色图像,如果你手动设置了特色图像,可以覆盖这段代码,这样即使你忘记了设置,wp会自动调用文章中的第一张图片作为缩略图

@H_502_0@

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

@H_502_0@问题来了,如果我们的文章里没有图片,又忘了设置特色图像呢,那么我们可以让WP调用媒体库里的某张指定的图片作为缩略图

@H_502_0@function wpforcefeatured() {

@H
5020@    global $post;

@H
502_0@    $already_has_thumb = has_postthumbnail($post->ID);

@H
502_0@    if (!$already_hasthumb)  {

@H
502_0@       $attached_image = get_children( "post_parent=$post->ID&post_type=attachment&post_mimetype=image&numberposts=1" );

@H
502_0@        if ($attachedimage) {

@H
502_0@           foreach ($attached_image as $attachmentid => $attachment) {

@H
502_0@              set_post_thumbnail($post->ID, $attachmentid);

@H
5020@            }

@H
5020@        } else {

@H
502_0@            set_postthumbnail($post->ID, '66');

@H
5020@        }

@H
5020@    }

@H
5020@}  //end function

@H
502_0@add_action('the_post', 'wpforcefeatured');

@H
502_0@其中 $post->ID,'66'是媒体库中某张图片的id

@H_5020@

@H
502_0@ 如何查看媒体库里某张图片的ID呢?

@H_5020@

@H
502_0@WP-后台-多媒体-媒体库,采用一个列表浏览方式,把鼠标指向图片,在浏览器的下面会显示图片的ID,或者你阅读下面这篇文章来实现:

@H_5020@

@H
502_0@wordpress后台无插件显示文章分类ID

@H_502_0@

自定义图片作为特色图像

@H_5020@/**

@H
502_0@  添加特色缩略图支持相关文章带有缩略图

@H_502_0@ 
/

@H_502_0@if ( function_exists('add_theme_support') )add_themesupport('post-thumbnails');

@H
502_0@//让你的主题开启特色图像功能

@H_502_0@function post_thumbnailsrc(){

@H
5020@global $post;

@H
502_0@if( $values = get_post_custom_values("thumb") ) {//输出自定义图片地址

@H_502_0@$values = get_post_customvalues("thumb");

@H
502_0@$post_thumbnailsrc = $values [0];

@H
502_0@} elseif( has_post_thumbnail() ){//如果有特色缩略图,则输出缩略图地址

@H_502_0@$thumbnail_src = wp_get_attachment_image_src(get_post_thumbnailid($post->ID),'thumbnail');

@H
502_0@//其中full可以修改的,可以为thumbnail,medium,large or full(分别代表最小的缩略图、中等、大和原始尺寸)

@H_502_0@$post_thumbnail_src = $thumbnailsrc [0];

@H
5020@} else {

@H
502_0@$post_thumbnailsrc = '';

@H
502_0@obstart();

@H
502_0@ob_endclean();

@H
502_0@$output = preg_match_all('/<img.+src='"['"].*>/i',$post->postcontent,$matches);

@H
502_0@$post_thumbnail_src = $matches [1] [0]; //获取图片 src

@H_502_0@if(empty($post_thumbnail_src)){ //如果日志中没有图片,则显示随机图片

@H_502_0@$random = mtrand(1,10);

@H
502_0@echo get_bloginfo('templateurl');

@H
5020@echo '/images/pic/'.$random.'.jpg';

@H
502_0@//如果日志中没有图片,则显示默认图片

@H_502_0@//echo '/images/defaultthumb.jpg';

@H
5020@}

@H
5020@};

@H
502_0@echo $post_thumbnailsrc;

@H
5020@}

@H
502_0@我们在主题下建立图像文件夹/images/pic/,里面上传你先显示图片,作为随机调用,而/images/default_thumb.jpg最为默认调用,当然这两个功能我们选择一个就OK了,所以注释掉了一个。

@H_502_0@

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

@H_502_0@ 要让你的主题支持特色图像必须在functions.PHP中加入以下代码

@H_502_0@if ( function_exists('add_theme_support') )add_themesupport('post-thumbnails');

@H
502_0@ 在post模板中调用:

@H_502_0@<?PHP

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

@H
502_0@  the_postthumbnail();

@H
5020@}

@H
5020@?>

@H
502_0@<?PHP thecontent(); ?>

@H
502_0@ 可以调用不同尺寸的图片:

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

@H_502_0@the_postthumbnail('thumbnail');// Thumbnail(默认尺寸 150px150px max)

@H
502_0@the_postthumbnail('medium');   // Medium resolution(default300px300px max)

@H
502_0@the_postthumbnail('large');    // Large resolution(default640px640px max)

@H
502_0@the_postthumbnail('full');     // Full resolution(original size uploaded)

@H
502_0@the_postthumbnail( array(100,100) );//Other resolutions

@H
5020@

总结

@H
502_0@1、你如果再制作自己的主题,在调试中,可以尝试一个你喜欢的方法

@H_5020@

@H
502_0@2、你的站点已经发表了很多文章,并且都启用发特色图像,要慎用,可能你选择的某一个代码功能会影响你已经发布的文章不能正常显示

@H_5020@

@H
502_0@3、特色图像会占用大量的服务器空间,因为每张图片都会裁剪成多张大小不同的缩略图方便在不同的位置调用,最主要的是不支持外链

猜你在找的wordpress相关文章