以下代码将更改特定帖子类型的上载目录!
只需确保将“post type”(第14行)的两个实例都替换为自定义post类型的名称即可。
/**
* Change Upload Directory for Custom Post-Type
*
* This will change the upload directory for a custom post-type. Attachments will
* now be uploaded to an "uploads" directory within the folder of your plugin. Make
* sure you swap out "post-type" in the if-statement with the appropriate value...
*/
function custom_upload_directory( $args ) {
$id = $_REQUEST[\'post_id\'];
$parent = get_post( $id )->post_parent;
// Check the post-type of the current post
if( "post-type" == get_post_type( $id ) || "post-type" == get_post_type( $parent ) ) {
$args[\'path\'] = plugin_dir_path(__FILE__) . "uploads";
$args[\'url\'] = plugin_dir_url(__FILE__) . "uploads";
$args[\'basedir\'] = plugin_dir_path(__FILE__) . "uploads";
$args[\'baseurl\'] = plugin_dir_url(__FILE__) . "uploads";
}
return $args;
}
add_filter( \'upload_dir\', \'custom_upload_directory\' );
这是为了与插件一起使用,但可以修改以用于主题。希望这有帮助,如果你有任何问题,请告诉我!
==更新==
我忘了提到,如果您想使用WordPress的默认年/月方法组织上传文件夹,只需将第15行和第16行更改为以下内容:
$args[\'path\'] = plugin_dir_path(__FILE__) . "uploads" . $args[\'subdir\'];
$args[\'url\'] = plugin_dir_url(__FILE__) . "uploads" . $args[\'subdir\'];