Wordpress dynamic child page without 404

Viewed 184

I want to create a subpage, e.g. /test/ and everything that I enter after /test/ should have one specific template and indexing, no 404 error.

I wanted to make this with virtual pages, but it's too many url's to add. (tried here - Wordpress fake/virtual folder)

I've got my template page-pagename.php which works. Now I need to add that every child of test does not return 404.

I think I have already searched the entire internet and cannot find a solution to this task

1 Answers

What you could fiddle with is with grabbing the main query on 'template_include'. You add an action like this. You can add it to your child theme in functions or in your custom plugin.

add_action( 'template_include', 'custom_router' );

Then in the custom router function, you can check the parameters that you are requesting ('test') and redirect to a template of your choice. Also add in functions.php or in custom plugin. Place a template file in the relevant path.

function custom_router( $query ) {
     global $wp_query;

        if($wp_query->query['name'] == 'test') :


        var_dump($wp_query->query['name']);
        var_dump($wp_query->query['page']);

        return dirname( __FILE__ ) . '/some-template.php';

        endif;

}

I tested the code in the latest wordpress version with a custom plugin and the default theme btw.

Related