我正在尝试将某些文件上载到以下自定义控制器:
/wp-content/uploads/audio/yyyy/mm
但是,未正确创建年和月目录。他们的表现如下:
/wp-content/uploads/audio/%25year%25/%25month%25/
这是我试图使用的代码。
function custom_upload_directory( $path ) {
$id = $_REQUEST[\'post_id\'];
$parent = get_post( $id )->post_parent;
// Check the post-type of the current post
if( "risen_multimedia" == get_post_type( $id ) || "risen_multimedia" == get_post_type( $parent ) ) {
$customdir = "/audio";
$time = current_time( \'mysql\' );
$y = substr( $time, 0, 4 );
$m = substr( $time, 5, 2 );
$subdirectory = "/$y/$m";
$path[\'path\'] = str_replace($path[\'subdir\'], \'\', $path[\'path\']); //remove default subdir (year/month)
$path[\'url\'] = str_replace($path[\'subdir\'], \'\', $path[\'url\']);
$path[\'subdir\'] = $customdir . $subdirectory;
$path[\'path\'] .= $path[\'basedir\'] . $customdir . $subdirectory;
$path[\'url\'] .= $path[\'baseurl\'] . $customdir . $subdirectory;
return $path;
}
return $path;
}
add_filter( \'upload_dir\', \'custom_upload_directory\' );
所以我不知道为什么年和月会显示为带%符号的单词,而不是整数?
SO网友:Jason Murray
目前无法测试,但我会尝试将日期重写为变量代码,从:
$time = current_time( \'mysql\' );
$y = substr( $time, 0, 4 );
$m = substr( $time, 5, 2 );
$subdirectory = "/$y/$m";
收件人:
$y = date(\'Y\');
$m = date(\'m\');
$subdirectory = "/" . $y . "/" . $m;