im尝试使用upload\\U dir筛选器
我使用此功能检查当前CPT
function get_current_post_type() {
global $post, $typenow, $current_screen;
//we have a post so we can just get the post type from that
if ( $post && $post->post_type ) {
return $post->post_type;
} //check the global $typenow - set in admin.php
elseif ( $typenow ) {
return $typenow;
} //check the global $current_screen object - set in sceen.php
elseif ( $current_screen && $current_screen->post_type ) {
return $current_screen->post_type;
} //lastly check the post_type querystring
elseif ( isset( $_REQUEST[\'post_type\'] ) ) {
return sanitize_key( $_REQUEST[\'post_type\'] );
}
//we do not know the post type!
return NULL;
}
现在,我想在名为“rsg\\U下载”的特定cpt上更改“upload\\u dir”
add_action( \'admin_init\', \'call_from_admin\' );
function call_from_admin() {
//Here i get the Current custom Post type is the Post type = "rsg_download" then i want upload in a other folder called "rsg-uploads"
$currentCPT = get_current_post_type();
if ( $currentCPT === \'rsg_download\' ) {
add_filter( \'upload_dir\', \'change_upload_dir\' );
}
}
当我只使用
$currentCPT = get_current_post_type();
if ( $currentCPT === \'rsg_download\' ) {
add_filter( \'upload_dir\', \'change_upload_dir\' );
}
“change\\u upload\\u dir”函数被调用了两次不知道为什么我也用“call\\u from\\u admin”函数从“admin\\u init”调用这个函数,到目前为止它只调用了一次很好
我转到我的CPT“rsg\\u下载”,上传文件在wp-content/uploads/rsg-uploads/this-works中的正确位置
现在我转到“页面”并上载文件,但我希望文件不在/rsg上载中,而是在默认路径中
函数更改upload\\u目录只有在自定义帖子类型为“rsg\\u download”时才应调用此函数:
function change_upload_dir( $param ) {
$mydir = \'/rsg-uploads\';
$param[\'path\'] = $param[\'basedir\'] . $mydir;
$param[\'url\'] = $param[\'baseurl\'] . $mydir;
return $param;
}
在freenode中从#wordpress获得了一些帮助,但仍然不适用于我:/Current code我尝试上载文件夹仍然是标准文件夹:
function get_current_post_type() {
global $post, $typenow, $current_screen;
if ( $post && $post->post_type )
return $post->post_type;
elseif( $typenow )
return $typenow;
elseif( $current_screen && $current_screen->post_type )
return $current_screen->post_type;
elseif( isset( $_REQUEST[\'post_type\'] ) )
return sanitize_key( $_REQUEST[\'post_type\'] );
return null;
}
function custom_post_type_upload_directory( $args ) {
if( \'rsg_download\' == get_current_post_type() ) {
$mydir = \'/rsg-uploads\';
$args[\'path\'] = $args[\'basedir\'] . $mydir;
$args[\'url\'] = $args[\'baseurl\'] . $mydir;
}
return $args;
}
add_filter( \'upload_dir\', \'custom_post_type_upload_directory\' );