0

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.

2
  • Have you tried endpoints? Commented Aug 9, 2013 at 8:56
  • @toscho endpoints don't suit in this case... Commented Aug 9, 2013 at 10:23

1 Answer 1

0

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.

4
  • Explain me please, why there is wpse8170 keyword ? Commented Aug 9, 2013 at 10:39
  • I have already did it, but I want to 'consume' projects in specified Page. So I have to fetch project in /projects/project-slug but projects post has to be available too. Commented Aug 9, 2013 at 10:41
  • @hsz wpse8170 is unique prefix which prevents collision issues. You can pick up your own unique prefix and use it in your development. Commented Aug 9, 2013 at 11:35
  • @hsz use archive-wpse8170-projects.php template to render archive page for project posts. In this template, fetch projects post/page and render it's content instead of rendering projects list. Commented Aug 9, 2013 at 11:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.