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:

Read More
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)

Related posts

Leave a Reply

3 comments

  1. 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.

  2. 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.

  3. 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.