是的,是的。就在昨天,我不得不做一些类似的事情,并找到了这个解决方案。与链接解决方案大致相同,但有更多的错误检查。
<?php
add_filter(\'upload_dir\', \'cgg_upload_dir\');
function cgg_upload_dir($dir)
{
// xxx Lots of $_REQUEST usage in here, not a great idea.
// Are we where we want to be?
if (!isset($_REQUEST[\'action\']) || \'upload-attachment\' !== $_REQUEST[\'action\']) {
return $dir;
}
// make sure we have a post ID
if (!isset($_REQUEST[\'post_id\'])) {
return $dir;
}
// modify the path and url.
$type = get_post_type($_REQUEST[\'post_id\']);
$uploads = apply_filters("{$type}_upload_directory", $type);
$dir[\'path\'] = path_join($dir[\'basedir\'], $uploads);
$dir[\'url\'] = path_join($dir[\'baseurl\'], $uploads);
return $dir;
}