是否可以将附件页段更改为引用的文件名?简而言之。我使用Gallery Shortcode构建一个简单的基于页面的图库。
我在上传过程中更改了原始文件名(如DSC1223.jpg) (为3b1871561aab.jpg),但它在url中不显示为slug。它只使用DSC1223。
有什么办法可以改变它吗?
致敬,史蒂夫
最好的方法是在我的functions.php中编写类似下面这样的代码
function twentyten_filter_wp_handle_upload () {
$upload = wp_handle_upload();
}
add_filter( 'wp_handle_upload', 'twentyten_filter_wp_handle_upload', 10, 2);发布于 2010-07-17 22:30:31
将此代码添加到hash upload filename plugin中,您就可以开始工作了;
/**
* 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函数来获取路径组件,并去掉目录路径和文件扩展名。
然后调用wp_update_post(),更新数据库中附件的post_name。
https://stackoverflow.com/questions/3265350
复制相似问题