因为主题制作者不统一的原因,wordpress你换个主题可能原来的特色图案就没了,而很多主题的功能和完整性和特色图案有息息相关的关系。
今天571免费资源小编介绍两种自动为新老文章设置特色图案的方法。
插件
插件最简单,安装上就行了,这里推荐免费的Easy Add Thumbnail插件,更新很勤快,已经完全兼容wordpress最新版(目前是5.8).
→ 点此获取正规游戏试玩赚钱软件~
直接后台插件——安装插件——搜索Easy Add Thumbnail即可,安装完毕无需设置,有人访问你的老文章,他就会自动给你设置一个特色图案,新文章你不设置的话,他也会在别人访问的时候自动将文章第一张图片设置为特色图案。
代码
另外一种办法就是添加functions代码。
代码如下:
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');
这段代码实现的功能是自动实现缩略图特色图案,比较推荐大家使用插件功能,不然主题更新还要再次修改代码,麻烦。
暂无评论