How to remove parent slugs from child pages permalinks?

To 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.

  1. 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 . "https://wordpress.stackexchange.com/";
    }
    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.

  1. 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.

  1. 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.

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)