wordpress – load a template based on the URI

I have a number of custom of post types – i.e. 'windows', 'doors', 'garages'

and I want to load a custom template (special.php) whenever my wordpress site has a url that looks like this:

www.mysite.com/windows/
www.mysite.com/doors/
www.mysite.com/garages/

I don’t want to use pages, as it is not appropriate in this case (I have given a scaled down example in the above example)

How can I achieve this?

Currently, as no pages exist with those names, I am getting an 404 (index.php)

3 Answers
3

I assume that your custom post type windows would map onto /windows/ etc?

You can customise the template for individual custom post types via single-posttype.php in your theme, e.g. single-windows.php single-doors.php and single-garages.php WordPress will automatically pick these up

You could also use custom page templates, e.g. page-windows.php or custom templates with the right template names.

If the pages are intended to list the post types, then you could try creating post archive templates, e.g.: http://mark.mcwilliams.me/2010/10/wordpress-3-1-introduces-custom-post-type-archives/

Or, you could create a taxonomy for each of said names, and use taxonomy-windows.php

Using all fo the above, one could share the code using something along the lines of:

<?php
// example code, may need minor modifications
get_template_part('special',$post->post_type);

Allowing you to share the code for all the pages in one file, and giving you child theme support, and the ability to override via files such as special-windows.php

OR, if none of the above suit you, there is a final solution:

http://www.braindonor.net/coding-blog/custom-pages-with-wordpress-plugins/230/

This will let you put a page with whatever you want, wherever you want, without needing any posts or pages of any kind, in a theme independent way.

Leave a Comment