WordPress如何自动给文章添加特色图像
之前追格小程序小编写过一篇《WordPress批量移除所有文章的特色图片方法》,而今天要讲的是,如何自动给文章添加特色图像,有时候偷懒懒得设置,有没有什么办法,直接获取正文中的第一张图为特色图像。
不妨看看小编是怎么操作的,先看效果图:
在WordPress主题的functions.php文件至下方的“?>”前,加入以下PHP代码即可。
if ( ! function_exists( 'fb_set_featured_image' ) ) {
add_action( 'save_post', 'fb_set_featured_image' );
function fb_set_featured_image() {
if ( ! isset( $GLOBALS['post']->ID ) )
return NULL;
if ( has_post_thumbnail( get_the_ID() ) )
return NULL;
$args = array(
'numberposts' => 1,
'order' => 'ASC', // DESC for the last image
'post_mime_type' => 'image',
'post_parent' => get_the_ID(),
'post_status' => NULL,
'post_type' => 'attachment'
);
$attached_image = get_children( $args );
if ( $attached_image ) {
foreach ( $attached_image as $attachment_id => $attachment )
set_post_thumbnail( get_the_ID(), $attachment_id );
}
}
}
-
WordPress函数:add_plugins_page 在后台侧边栏-插件,添加子菜单使用函数 add_plugins_page 在WordPress侧边栏-插件下添加子菜单
-
WordPress函数:add_users_page 在后台侧边栏-用户,添加子菜单WordPress函数 add_users_page 在后台侧边栏【用户】下添加子菜单
-
WordPress函数:add_options_page 在后台侧边栏-设置,添加子菜单WordPress函数 add_options_page 在后台侧边栏-设置,添加子菜单
-
WordPress分类URL自动添加斜杠有些小伙伴肯定开始疑问了,WordPress分类URL为什么要加斜杠,其实主要还是为了搜索引擎,利于SEO,带斜杠的URL样式如下
-
WordPress( .htaccess )如何禁止SQL查询或禁止垃圾信息请求WordPress如何禁止SQL查询或禁止垃圾信息请求,若我们使用Apache引擎或LAMP环境,我们只需要在根目录的 .htaccess 文件中添加以下代码即可。
-
WordPress函数:wp_generate_uuid4 和 wp_is_uuid 生成和判断UUIDWordPress提供了生成和判断UUID的函数
暂无评论,抢个沙发...