
wordpress如何自动把第一张图片设置为特色图片,下面web建站小编给大家详细介绍一下具体实现代码!
在当前主题的functions.php文件中新增以下代码:
if ( ! function_exists( 'auto_set_featured_image' ) ) {
add_action( 'save_post', 'auto_set_featured_image' );
function auto_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', //排序 ASC/DESC
'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建站如何快速提交和优化您的网站(IndexNow插件)?
标签: wordpress技巧, wordpress特色图片
上面是“wordpress如何自动添加特色图片”的全面内容,想了解更多关于 wordpress 内容,请继续关注web建站教程。
当前网址:https://m.ipkd.cn/webs_3750.html
声明:本站提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请发送到邮箱:admin@ipkd.cn,我们会在看到邮件的第一时间内为您处理!

git教程之多人协作
如何查看网站是否被百度处罚?
Masterpiece X