這是一個遲到的答案,但我想給的答案更清潔的版本alesub
了。
function wp21418_append_to_post_name() {
// Checks to see if the option images_updated has been set or has a value of true.
if (get_option('images_updated') === 'true') :
return;
endif;
// get all attachment posts.
$attachments = get_posts([
'post_type' => 'attachment',
'post_status' => 'inherit',
'name' => $p->slug,
'posts_per_page' => -1,
]);
// For each attachment, loop and update the post_name
foreach($attachments as $p){
$attachment = array(
'ID' => $p->ID,
'post_name' => $p->post_name.'-image'
);
// Update the post into the database
wp_update_post($attachment);
}
// Once everything is looped, add the option to the database.
add_option('images_updated', 'true');
}
add_action('after_setup_theme', 'wp21418_append_to_post_name');
該函數在主題設置後立即在動作鉤子上運行。第一行檢查數據庫images_updated
中是否有選項。如果存在這種選擇,我們就保留該功能,而不做任何處理。否則,如果該選項不存在,它將運行該函數並在最後設置該選項。
這使得它,所以它只能運行一次。刷新後不必刪除該功能。如果您想再次運行它,可以簡單地刪除頂部的if
語句。 作爲一個警告:這樣做會在post_names
末尾添加另一個-image
即使他們有-image
已經(例如,-image-image
)
可能會有更大的文件名檢查這種情況。如果有人真的需要,請更新答案。
我見過的附加「-1」之前,但我仍然感到困惑爲什麼沒有在我的故事發生。 WP根據頁面的標題自動生成衝突的slu//永久鏈接。同樣,主題如何允許/禁止媒體文件擁有自己的頁面?我絕對沒有故意啓用它。 – emersonthis 2012-03-22 13:17:04
在attachment.php下(http://codex.wordpress.org/images/1/18/Template_Hierarchy.png)。上傳的文件不應該與已經創建的slu conflict衝突,系統會自我保護(它也會爲圖片添加後綴),所以不知道發生了什麼,有時如果您更改了url結構,則必須刷新永久鏈接。 – Wyck 2012-03-22 14:31:57