自定义WordPress上载目录

时间:2015-02-23 作者:leclaeli

我正在尝试将某些文件上载到以下自定义控制器:

/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\' );
所以我不知道为什么年和月会显示为带%符号的单词,而不是整数?

1 个回复
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;

结束

相关推荐

Media upload error

我是WP开发的新手。媒体上传时突然出现上传错误。甚至我也停用了所有插件。现在我使用的是WP的最新版本(4.1)。错误消息后,图像上载成功(uploads/2014/12/image\\u name)。但未在管理面板中列出,并显示错误消息。有什么建议吗?