我最近还需要在一个网站上这样做。
如果您将标题字段保留为空,则WordPress会在保存期间将空标题替换为附件的名称。这是通过向过滤器添加函数来实现的attachment_fields_to_save
. 如果我们取消挂钩,则可以使用空标题保存附件:
// Remove WordPress built in filter that filles post name with attachment filename if title is empty on save
// We use prio 5 on our filter because we need to call our function before wordpress calls its own
add_filter( \'attachment_fields_to_save\', \'unhook_attachment_fields_to_save\', 5, 2 );
function unhook_attachment_fields_to_save( $post, $attachment ) {
// "image_attachment_fields_to_save" is the name of the function
// that wordpress did add
remove_filter( \'attachment_fields_to_save\', \'image_attachment_fields_to_save\', 10 );
return $post;
}