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.

本文标签: