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)
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:
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.
move all your script in single.php to anthor template called say “narmol.php”
and in the single.php, place a condition like
the above code is just an idea(Pseudocode). I assume you know how to implement it. let me know if you dont know.
When you register new post type, there’s an argument that will enable the post type archives
has_archive
(check the Codex). When you enable post type archive, these urls:will uses
archive-$post_type.php
as the template file. And if the filearchive-$post_type.php
doesn’t exist, it will looks forarchive.php
. (check the Codex)So I think a simple solution for you is checking the
$post_type
inarchive.php
file and when it’s one ofwindows, doors, garages
then we includespecial.php
, like this:Note: Don’t forget to set
has_archive
totrue
when you register post type.