如果你的站点文章数量很多,那么每一篇文章都要手动设置缩略图必然会是个繁杂的步骤,自动获取文章内第一张图片做为缩略图。
这将大大减少我们的工作量,很方便的功能,代码分为两部分,根据自己需求选择,可以只用前端,也可以前后端一起用,代码教程下如:
一,设置网站前端缩略图
//自动获取文章内第一张图片做为缩略图 function catch_that_image( $id ) { // global $post, $posts; $first_img = ''; // 如果设置了缩略图 $post_thumbnail_id = get_post_thumbnail_id( $id ); if ( $post_thumbnail_id ) { $output = wp_get_attachment_image_src( $post_thumbnail_id, 'large' ); $first_img = $output[0]; } if(empty($first_img)){ //Defines a default image $first_img = "/wp-content/themes/img/default.png"; } return $first_img; }
1,将上面代码添加到自己主题的functions.php
文件。
<?php echo catch_that_image( $post->ID ); ?>
2,将上面代码添加到需要获取缩略图的文章循环中即可。
上面这个显示的是图片的原图地址,如果需要用到图片裁剪等功能,建议将图片放到云存储,裁剪功能很方便。
如果你的图片在云存储又要裁剪到你需要的尺寸,请将代码换成:
<?php echo catch_that_image( $post->ID )
,'?imageMogr2/gravity/center/crop/110x110/interlace/0';?>
我用的是腾讯COS,如果是其他云存储,请更换红色部分的裁剪规则。
以上代码只是网站前端文章列表显示缩略图,后台是没有的,就是说你编辑文章的时候特色图像区域是没有图片的,如果后台也要一起设置,请加入以下代码。
二,设置后台缩略图
//给WordPress文章设置缩略图,并写入数据库 function easy_add_thumbnail( $post ) { $already_has_thumb = has_post_thumbnail(); $post_type = get_post_type( $post->ID ); $exclude_types = array( '' ); $exclude_types = apply_filters( 'eat_exclude_types', $exclude_types ); // Do nothing if the post has already a featured image set. if ( $already_has_thumb ) { return; } // Do the job if the post is not from an excluded type. if ( ! in_array( $post_type, $exclude_types, true ) ) { // Get first attached image. $attached_image = get_children( "order=ASC&post_parent=$post->ID&post_type=attachment&post_mime_type=image&numberposts=1" ); if ( $attached_image ) { $attachment_values = array_values( $attached_image ); // Add attachment ID. add_post_meta( $post->ID, '_thumbnail_id', $attachment_values[0]->ID, true ); } } } // Set featured image before post is displayed on the site front-end (for old posts published before enabling this plugin). add_action( 'the_post', 'easy_add_thumbnail' ); // Hooks added to set the thumbnail when publishing too. add_action( 'new_to_publish', 'easy_add_thumbnail' ); add_action( 'draft_to_publish', 'easy_add_thumbnail' ); add_action( 'pending_to_publish', 'easy_add_thumbnail' ); add_action( 'future_to_publish', 'easy_add_thumbnail' );
将上面的代码添加到当前主题的functions.php
中即可。
如果当前文章中没有图片,但又想显示一张默认的缩略图该怎么办,可以将上面的代码修改一下换成下面的代码,调用媒体库中某个图片作为默认的缩略图:
//给WordPress文章设置缩略图,并写入数据库
function easy_add_thumbnail( $post ) {
$already_has_thumb = has_post_thumbnail();
$post_type = get_post_type( $post->ID );
$exclude_types = array( '' );
$exclude_types = apply_filters( 'eat_exclude_types', $exclude_types );
// Do nothing if the post has already a featured image set.
if ( $already_has_thumb ) {
return;
}
// Do the job if the post is not from an excluded type.
if ( ! in_array( $post_type, $exclude_types, true ) ) {
// Get first attached image.
$attached_image = get_children( "order=ASC&post_parent=$post->ID&post_type=attachment&post_mime_type=image&numberposts=1" );
if ( $attached_image ) {
$attachment_values = array_values( $attached_image );
// Add attachment ID.
add_post_meta( $post->ID, '_thumbnail_id', $attachment_values[0]->ID, true );
}else {
set_post_thumbnail($post->ID, '1530');
}
}
}
// Set featured image before post is displayed on the site front-end (for old posts published before enabling this plugin).
add_action( 'the_post', 'easy_add_thumbnail' );
// Hooks added to set the thumbnail when publishing too.
add_action( 'new_to_publish', 'easy_add_thumbnail' );
add_action( 'draft_to_publish', 'easy_add_thumbnail' );
add_action( 'pending_to_publish', 'easy_add_thumbnail' );
add_action( 'future_to_publish', 'easy_add_thumbnail' );
其中的红色部分数字1530,是媒体库中某个图片附件的ID号,前端和后台共用一张图片就可以了,根据自己需求自行设置,最好是同一张,不然后台和前端缩略图会不一样。
上面的代码只是一篇技术文章,可能会影响到之前添加的特色图像,所以不要轻易在自己的网站上做试验。
至此,本文结束。