我有這是 WordPress 帖子編輯網址:
https://example.com/wp-admin/post.php?post=ID&action=edit
我想將其更改為Slug而不是這樣的ID:
https://example.com/wp-admin/post.php?post=Slug&action=edit
我試圖用這個 URL 編輯帖子但沒有作業:
https://example.com/wp-admin/post.php?post=MyPostSlug&action=edit
uj5u.com熱心網友回復:
為了更改編輯帖子鏈接結構,您可以get_edit_post_link像這樣使用過濾器:
add_filter( 'get_edit_post_link', 'so_73914075_get_edit_post_link', 10, 3);
function so_73914075_get_edit_post_link($link, $post_id, $context) {
$post = get_post( $post_id );
if ( ! in_array( $post->post_type, array( 'post', 'page' ) ) ) {
return $link;
}
$post_type_object = get_post_type_object( $post->post_type );
if ( 'revision' === $post->post_type ) {
$action = '';
} elseif ( 'display' === $context ) {
$action = '&action=edit';
} else {
$action = '&action=edit';
}
if ( 'display' === $context ) {
$post_type = '&post-type=' . $post->post_type;
} else {
$post_type = '&post-type=' . $post->post_type;
}
$custom_edit_link = str_replace( '?post=%d', '?post-name=%s', $post_type_object->_edit_link );
return admin_url( sprintf( $custom_edit_link . $action . $post_type, $post->post_name ) );
}
這會將常規帖子和頁面的編輯鏈接更改為以下內容:
https://example.com/wp-admin/post.php?post-name=the-post-slug&action=edit&post-type=post
警告:確保將其限制為僅需要更改 URL 的帖子型別。使這種更改全域化幾乎肯定會對其他插件產生意想不到的影響
然而,讓管理面板真正加載編輯螢屏并不是那么容易。
查看wp-admin/post.php檔案的源代碼,您會發現無法掛鉤到流中并$post_id使用與您發送的 slug 匹配的 post id 填充變數。
這意味著您有兩個選擇:
- 推薦將編輯鏈接更新為您想要的任何格式,然后創建一個管理員重定向功能,該功能從初始 URL 中提取 slug 并重定向到其中包含的實際編輯頁面
?post=%d。 - 不推薦創建一個新的管理員編輯頁面,該頁面將了解您的 URL 結構,并在基于 slug
wp-admin/post.php的代碼之后包含。$post_id
第二種方法可能帶有許多額外的代碼,您需要撰寫以涵蓋所有案例和帖子post.php在管理面板中到達的所有實體
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/511388.html
