问题描述
是否可以将附件页面的标签更改为引用文件名?简而言之...我使用画廊简码来构建一个基于页面的简单画廊.
Is it possible to change the attachment page slug to the referring filename? In short... i use the Gallery Shortcode to build a simple page based gallery.
我在上载过程中将原始文件名(如DSC1223.jpg)更改为3b1871561aab.jpg,但它并不适合url中的所有内容.它仅使用DSC1223.
I change the orignal filename (like DSC1223.jpg) during the upload process (to 3b1871561aab.jpg) but it does not appery as slug within the url. It uses only DSC1223.
反正有改变IST吗?
关于,史蒂夫
最好的方法是在我的functions.php中编写类似的内容
The best way would be to write something like this in my functions.php
function twentyten_filter_wp_handle_upload () {
$upload = wp_handle_upload();
}
add_filter( 'wp_handle_upload', 'twentyten_filter_wp_handle_upload', 10, 2);
推荐答案
将此添加到哈希上传文件名插件并且您应该会很好;
Add this to the hash upload filename plugin and you should be good to go;
/**
* 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');
请随时询问您是否不确定每一行的作用!
Don't hesitate to ask if you're not sure what each line does!
更新:
在将新附件保存到数据库之后,此函数就挂接到add_attachment
事件.从wp_insert_attachment()
内部调用此操作.
This function hooks onto the add_attachment
event, just after a new attachment is saved to the database. This action is called from within wp_insert_attachment()
.
我们首先获取附件(get_attached_file()
)的文件名.然后,我们使用本机PHP函数 pathinfo() 获取路径组件,并去除目录路径和文件扩展名.
We first grab the filename of the attachment (get_attached_file()
). Then we use a native PHP function pathinfo() to get the path components, and strip the directory path and file extension away.
然后我们调用wp_update_post()
,更新数据库中附件的post_name
.
Then we call wp_update_post()
, updating the post_name
of the attachment in the database.
这篇关于将Wordpress Gallery短代码段更改为文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!