默认WordPress自定义媒体上传路径wp-content\uploads并以年和月文件夹归档
方法一:通过add_filter 钩子修改上传路径
- function slider_upload_dir($uploads) {
- $siteurl = get_option( ‘siteurl’ );
- $uploads[‘path’] = WP_CONTENT_DIR . ‘/slider’;
- $uploads[‘url’] = $siteurl . ‘/wp-content/slider’;
- $uploads[‘subdir’] = ”;
- $uploads[‘basedir’] = $uploads[‘path’];
- $uploads[‘baseurl’] = $uploads[‘url’];
- $uploads[‘error’] = false;
- return $uploads;
- }
- add_filter(‘upload_dir’, ‘slider_upload_dir’);
之后媒体上传路径会修改为:wp-content/slider,自己可以修改目录名称。
方法二:找回WordPress隐藏的上传路径选项。
将下面的代码添加到当前主题functions.php文件中,就可以调出该选项:
- if(get_option(‘upload_path’)==‘wp-content/uploads’ || get_option(‘upload_path’)==null) {
- update_option(‘upload_path’,WP_CONTENT_DIR.‘/uploads’);
- }
需要注意的是,上传路径只能写相对路径,例如:wp-content/uploads/img