如何将图像缩略图移动到自定义文件夹中以自定义帖子类型

时间:2017-01-04 作者:Richard Tinkler

我改编了菲利普的答案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\');
提前感谢您的帮助!

1 个回复
最合适的回答,由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

相关推荐

Remove <p></p> after images

我对<p></p> 出现在my之后的标记<img....>. 以下是我在本地主机上查看生成的页面时显示的内容。。。<img src=\"/wp-content/themes/wunderful/assets/images/feedback-danielle.png\" alt=\"Danielle Johnson Deal Town FC Treasurer\"> <p></p> 请注意随机生成的<p>&