Extend page routing with custom parameters

In WordPress there is defined a simple routing

/%postname%/

So when I access /projects/ it loads me Projects page which has specified custom template_projects.php template.

Is it possible to extend routing but only for this template ?

Let's say I want to extend routing to handle project id, so it should be:

/%postname%/%project-id%/

But other pages will work with base route.

Topic routing Wordpress

Category Web


In your case you need to register custom post type for your projects. You can do it by using register_post_type function:

add_action( 'init', 'wpse8170_register_post_type' );
function wpse8170_register_post_type() {
    register_post_type( 'wpse8170-projects', array(
        'label'   => 'Projects',
        'public'  => true,
        'rewrite' => array( 'slug' => 'project' ),
    ) );
}

Don't forget to flush rewrite rules.

After you register your custom post type, your projects will have /project/%wpse8170-projects% (%wpse8170-project% will be replaced by project slug) permalink structure and you will be able to use single-wpse8170-projects.php template for your projects.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.