你的条件有问题。
get_the_category()
返回object, 使用foreach查找特定类别。
此外,您还为$categories
变量,要进行比较,必须使用comparison operators (例如:==或==)
我重构了代码,并根据您的情况进行了调整,希望能有所帮助。
add_action( \'save_post\', \'wp_force_featured_image\', 10, 3 );
function wp_force_featured_image( $post_id, $post, $update ) {
if ( $update ) {
return;
}
if ( $post->post_type !== \'post\' ) {
return;
}
if ( $post->post_status !== \'publish\' ) {
return;
}
$has_post_thumbnail = get_post_thumbnail_id( $post_id );
if ( ! empty( $has_post_thumbnail ) ) {
return;
}
$categories = get_the_category( $post_id );
$thumbnail_id = false;
foreach ( $categories as $category ) {
if ( $category->slug === \'news\' ) {
$thumbnail_id = 3135;
break;
}
if ( $category->slug === \'bi\' ) {
$thumbnail_id = 3138;
break;
}
}
if( $thumbnail_id ) {
add_post_meta( $post_id, \'_thumbnail_id\', $thumbnail_id, true );
}
}