0

是否可以将附件页面 slug 更改为引用文件名?简而言之......我使用画廊简码来构建一个简单的基于页面的画廊。

我在上传过程中更改了原始文件名(如 DSC1223.jpg)(更改为 3b1871561aab.jpg),但它不会像 url 中的 slug 一样出现。它仅使用 DSC1223。

无论如何要改变ist?

问候,史蒂夫

最好的方法是在我的functions.php中写这样的东西

function twentyten_filter_wp_handle_upload () {

    $upload =  wp_handle_upload();

}

add_filter( 'wp_handle_upload', 'twentyten_filter_wp_handle_upload', 10, 2);
4

1 回答 1

2

将此添加到哈希上传文件名插件中,您应该一切顺利;

/**
 * Filter new attachments and set the post_name the same as the hashed
 * filename.
 * 
 * @param int $post_ID
 */
function change_attachment_name_to_hash($post_ID)
{
    $file = get_attached_file($post_ID);
    $info = pathinfo($file);
    $name = trim( substr($info['basename'], 0, -(1 + strlen($info['extension'])) ) );
    wp_update_post(array(
        'ID' => $post_ID,
        'post_name' => $name
    ));
}
add_action('add_attachment', 'change_attachment_name_to_hash');

如果您不确定每条线的作用,请不要犹豫!

更新:

add_attachment在将新附件保存到数据库之后,此函数会与事件挂钩。这个动作是从内部调用的wp_insert_attachment()

我们首先获取附件的文件名get_attached_file()( )。然后我们使用原生 PHP 函数pathinfo()来获取路径组件,并去除目录路径和文件扩展名。

然后我们调用wp_update_post(),更新post_name数据库中的附件。

于 2010-07-17T14:30:30.597 回答