admin管理员组文章数量:1127989
I remove them all right, but when I actually try to go to a page, there is a 404 error.
The code I have used ( variation of Removing Parent Page(s) from Permalink )
function my_pages_permalink( $link, $post_id) {
$slugname = get_post_field( 'post_name', $post_id, 'display' );
$slugname = $slugname."/";
$link = untrailingslashit( home_url($slugname) ) . '.html';
return $link;
}
add_filter( 'page_link', 'my_pages_permalink', 10, 3 );
What else should be done? Some add_rewrite_rule? What it may be like?
I remove them all right, but when I actually try to go to a page, there is a 404 error.
The code I have used ( variation of Removing Parent Page(s) from Permalink )
function my_pages_permalink( $link, $post_id) {
$slugname = get_post_field( 'post_name', $post_id, 'display' );
$slugname = $slugname."/";
$link = untrailingslashit( home_url($slugname) ) . '.html';
return $link;
}
add_filter( 'page_link', 'my_pages_permalink', 10, 3 );
What else should be done? Some add_rewrite_rule? What it may be like?
Share Improve this question asked Dec 23, 2023 at 17:07 BonMoDoBonMoDo 113 bronze badges 3- Did you flush the permalinks after adding your code? – Pat J Commented Dec 24, 2023 at 14:28
- Sure, I did flush it. – BonMoDo Commented Dec 24, 2023 at 18:53
- @PatJ It seems WordPress somehow does not know or understand that a new permalink structure is set. Since new links (without parent slugs) are right in their proper places in resulting page code, but clicking on them does not lead to the corresponding pages, but to 404 error. – BonMoDo Commented Dec 24, 2023 at 19:01
1 Answer
Reset to default 0To remove parent slugs from child pages' permalinks in WordPress and avoid the 404 error, you'll need to modify your code and add some rewrite rules.
- First, modify your code to remove the parent slug from the child page's permalink:
function my_pages_permalink($link, $post) {
if ($post->post_parent) {
$parent = get_post($post->post_parent);
$link = trailingslashit(home_url($parent->post_name)) . $post->post_name . '/';
}
return $link;
}
add_filter('page_link', 'my_pages_permalink', 10, 2);
This code checks if the page has a parent and constructs the permalink accordingly.
- Next, you need to add rewrite rules to handle the modified permalinks. Add the following code to your theme's
functions.php
or create a custom plugin:
function custom_rewrite_rules() {
add_rewrite_rule(
'([^/]+)/([^/]+)/?$',
'index.php?pagename=$matches[2]',
'top'
);
}
add_action('init', 'custom_rewrite_rules');
These rewrite rules will interpret the modified permalink structure.
- After adding the rewrite rules, you need to flush the rewrite rules for them to take effect. You can do this by visiting the "Settings" > "Permalinks" page in your WordPress admin and simply clicking the "Save Changes" button.
With these modifications, the parent slugs should be removed from child pages' permalinks, and you should no longer encounter the 404 error. This solution works by rewriting the URLs to match the new structure you defined in your my_pages_permalink
function.
本文标签: How to remove parent slugs from child pages permalinks
版权声明:本文标题:How to remove parent slugs from child pages permalinks? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736707089a1948736.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论