未经测试,但从理论上讲应该可行:
add_filter(\'get_attached_file\', function($path, $file, $attachment_id){
// get the post object of the current attachment
$att = get_post($attachment_id);
// prepend attachment post parent ID to the file name
return substr_replace($path, "{$att->post_parent}/{$file}", -strlen($file));
});
另一个过滤器尝试“修复”WP的上载处理程序返回的路径:
add_filter(\'wp_handle_upload\', function($results){
global $post;
if(empty($post))
return $results;
extract($results);
$uploads = wp_upload_dir();
$file = str_replace($uploads[\'basedir\'], "{$uploads[\'basedir\']}/{$post->ID}", $file);
$url = str_replace($uploads[\'baseurl\'], "{$uploads[\'baseurl\']}/{$post->ID}", $url);
return compact(\'file\', \'url\', \'type\');
});
如果媒体上载程序不在全局$post变量中公开当前帖子,则此操作将不起作用。