wordpress 默認(rèn)的上傳目錄位于 wp-content/uploads ,并且根據(jù)設(shè)置還可以按照年月歸檔??墒牵绻乙蟼饕粋€(gè)頭像,或者幻燈片的話(huà),也跟附件混在一起總是不太好吧?幸好 wordpress 知道會(huì)有用戶(hù)有這種需求,我們可以通過(guò) filter 來(lái)臨時(shí)改變文件上傳目錄。filter 函數(shù)如下:
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;
}
在 wordpress 處理上傳文件之前,加載這個(gè) filter 函數(shù):
add_filter('upload_dir', 'slider_upload_dir');
這樣上傳的文件將會(huì)上傳到 wp-content/slider 目錄!這個(gè)方法特別適合于有上傳頭像或者幻燈片需求的項(xiàng)目,可以避免這些文件和文章附件混淆。