Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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)

share|improve this question

migrated from stackoverflow.com Jun 28 '11 at 14:05

This question came from our site for professional and enthusiast programmers.

    
hmmm perhaps this would have been better on the Wordpress Stack Exchange site –  Tom J Nowell Jun 28 '11 at 11:16

3 Answers 3

up vote 1 down vote accepted

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.

share|improve this answer

move all your script in single.php to anthor template called say "narmol.php"

and in the single.php, place a condition like

 if(post_type="windows" || "doors" || "garages") include "special.php";
 else include "normal.php";

the above code is just an idea(Pseudocode). I assume you know how to implement it. let me know if you dont know.

share|improve this answer
    
Or you could use the single-windows.php single-doors.php and single-garages.php templates? –  Tom J Nowell Jun 28 '11 at 11:14
1  
@Tom as I understand, if the post type is windows,doors or garages, it will load the same special.php. your solutions can be applied when they want to load different template for each post type –  bingjie2680 Jun 28 '11 at 11:16
    
If Wordpress is loading the template for a post of type 'windows' it will first attempt to load single-windows.php and if that fails it will load single.php –  Tom J Nowell Jun 28 '11 at 11:18
    
Your snippet of code is therefore completely unnecessary, and replicates existing wordpress functionality –  Tom J Nowell Jun 28 '11 at 11:18
    
afterall wouldnt it be easier to do put <?php get_template_part('special',post_type); in those files? Allowing for sharing of the same code, but with the option of child themes, and overriding? –  Tom J Nowell Jun 28 '11 at 11:19

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:

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

will uses archive-$post_type.php as the template file. And if the file archive-$post_type.php doesn't exist, it will looks for archive.php. (check the Codex)

So I think a simple solution for you is checking the $post_type in archive.php file and when it's one of windows, doors, garages then we include special.php, like this:

<?php
// archive.php
$post_type = get_query_var('post_type');
if (in_array($post_type, array('windows', 'doors', 'garages'))) {
    include 'special.php';
    exit;
}

// default content for other archive type
?>

Note: Don't forget to set has_archive to true when you register post type.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.