我改编了菲利普的答案here 将所有缩略图移动到自定义文件夹。我们如何使其适用于特定的自定义帖子类型?
function custom_rename_images($image) {
// Split the $image path
$info = pathinfo($image);
$dir = $info[\'dirname\'] . \'/\';
$ext = \'.\' . $info[\'extension\'];
$name = wp_basename($image, \'$ext\');
if(!is_dir($dir.\'_thumbs\')){
mkdir($dir.\'_thumbs\');
}
// New Name
$new_name = substr_replace($image,\'/_thumbs/\',strrpos($image,\'/\'),1);
// Rename the intermediate size
$did_it = rename($image, $new_name);
// Return if successful
if ($did_it) return $new_name;
// Return on fail
return $image;
//}
}
add_filter(\'image_make_intermediate_size\', \'custom_rename_images\');
提前感谢您的帮助!
最合适的回答,由SO网友:Mostafa Soufi 整理而成
在中使用波纹管过滤器functions.php
<?php
add_filter( \'upload_dir\', function($args) {
if( !isset($_REQUEST[\'post_id\']) ) {
return $args;
}
$post_type_name = get_post_type( $_REQUEST[\'post_id\'] );
if( $post_type_name = \'custom_post_type\' ) {
// Set the new path depends on current post_type
$newdir = \'/\' . $post_type_name;
$args[\'path\'] = str_replace( $args[\'subdir\'], \'\', $args[\'path\'] ); //remove default subdir
$args[\'url\'] = str_replace( $args[\'subdir\'], \'\', $args[\'url\'] );
$args[\'subdir\'] = $newdir;
$args[\'path\'] .= $newdir;
$args[\'url\'] .= $newdir;
return $args;
}
return $args;
} ); ?>
https://gist.github.com/mostafasoufi/c525572d9b4404808b0a7bbf14f09ffa