【发布时间】:2023-01-31 22:06:03
【问题描述】:
我正在尝试让帖子中的第一个 img 自动成为特色 img 这是我在主题编辑器的 functions.php 中添加的内容
// Auto add featured image
function wpsites_auto_set_featured_image() {
global $post;
$featured_image_exists = has_post_thumbnail($post->ID);
if (!$featured_image_exists) {
$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);}
}
}
}
add_action('the_post', 'wpsites_auto_set_featured_image');
有什么我需要添加或更改的吗 我对 php 比较陌生
【问题讨论】:
标签: wordpress wordpress-theming custom-wordpress-pages