admin管理员组文章数量:1125034
Is this a limitation inherent to WordPress? For instance, when we have a URL structure with three levels of hierarchy, is it not possible to remove the first level? Currently, I must maintain the permalink structure as "/%postname%/" as required. Specifically, I have a parent page titled "tournament-name" with children and grandchildren. When attempting to access a child page URL without including the grandparent URL slug but including the parent URL slug, it automatically redirects to include the grandparent URL slug.
For example: I need to access the existing page / using the URL / - that is without 'tournament-name' URL keyword.
This change is for search engine optimization.
I'm able to remove the tournament-name
and get it rediredted to /. But wordpress automatically redirects it back to / - leading to an infinite loop. Also I need to make sure the page / shows the same content as /
The code I tried is given below
function custom_remove_parent_page_slug() {
// List of parent pages for which you want to remove the slug
$parent_pages = array(
'tournament-name'
);
global $wp;
$requested_url = home_url( $wp->request );
foreach ( $parent_pages as $parent_page ) {
$parent_page_url = home_url( $parent_page );
// Check if the requested URL starts with the parent page URL
if ( strpos( $requested_url, $parent_page_url ) === 0 ) {
// Remove the parent page slug from the URL
$child_page_url = str_replace( $parent_page_url, home_url(), $requested_url );
// Check if the child page URL is different from the requested URL
if ( $child_page_url !== $requested_url ) {
// Redirect with a 301 status code
wp_redirect( $child_page_url, 301 );
exit;
}
}
}
}
add_action( 'parse_request', 'custom_remove_parent_page_slug' );
Please help me. Thanks in advance.
本文标签:
版权声明:本文标题:php - How can I eliminate the top-level URL slug in WordPress while still being able to access child pages, particularly three l 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736633143a1945821.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论