Title: Using A Custom Page Template Based On PHP Switch Statement
Last modified: August 20, 2016

---

# Using A Custom Page Template Based On PHP Switch Statement

 *  Resolved [kirkward](https://wordpress.org/support/users/kirkward/)
 * (@kirkward)
 * [13 years, 7 months ago](https://wordpress.org/support/topic/using-a-custom-page-template-based-on-php-switch-statement/)
 * Hi,
 * I am creating a Multsite Network for a particular group of users. The site includes
   a category hidden from the Site Admins in the WP dashboard. They cannot edit 
   posts in this category or add new posts to this category. Only the Super Admin
   can do that.
 * Because I am using a custom post template for this category, the list of custom
   post templates is available to the Site Admin on the Edit/Add Post page. My best
   solution (so far) has been to have several templates created in another folder
   and use the PHP Switch statement to select the correct template for the post 
   using a variable I have stored in the database.
 * Here is the code from my custom template:
 *     ```
       <?php
       /*
       Template Name Posts: Niche Posts
       */
       ?>
       <?php $niche = 'cow'; 
   
       switch ($niche) {
           case "store":
               include "http://mysite.com/wp-content/uploads/stores/store-template.php" ;
               break;
           case "bar":
               echo "i is bar";
               break;
           default:
       	include "http://mysite.com/wp-content/themes/mytheme/template-fullwidth.php") ;
               break;
       }
       ?>
       ```
   
 * I am returned a blank page.
 * The templates work properly when called directly, but not when I try to include
   them.
 * I have also tried using the ABSPATH function, with similar results.
 *     ```
       <?php include(ABSPATH . "../template-fullwidth.php") ; ?>
       ```
   
 * Any help someone can give would be appreciated.

Viewing 9 replies - 1 through 9 (of 9 total)

 *  [wpismypuppet](https://wordpress.org/support/users/wordpressismypuppet/)
 * (@wordpressismypuppet)
 * [13 years, 7 months ago](https://wordpress.org/support/topic/using-a-custom-page-template-based-on-php-switch-statement/#post-3142252)
 * Try using WordPress’ function [get_template_part()](http://codex.wordpress.org/Function_Reference/get_template_part).
   Though it sounds weird cause you aren’t really getting a “part” of a template,
   it is indeed what you want. So you would call:
 *     ```
       switch ($niche) {
           case "store":
               get_template_part( 'store', 'template' );
               break;
           case "bar":
               echo "i is bar";
               break;
           default:
       	get_template_part( 'template' , 'fullwidth' );
               break;
       }
       ```
   
 * Read up on the documentation to see why I pass what I pass, but this should work.
   The function basically does the include() function for you, but it knows the 
   right path, and it checks to make sure the file actually exists and does a graceful
   fail instead of PHP errors.
 *  Thread Starter [kirkward](https://wordpress.org/support/users/kirkward/)
 * (@kirkward)
 * [13 years, 7 months ago](https://wordpress.org/support/topic/using-a-custom-page-template-based-on-php-switch-statement/#post-3142265)
 * I had never heard of get_template_part before today (and as a non-coder, I couldn’t
   understand it).
 * Taking the line
 *     ```
       get_template_part( 'store', 'template' );
       ```
   
 * does that search until it finds the “store-template” in any directory or folder
   in wp-content?
 * My site admins will be able to change themes, and I do not want to have to copy
   all the templates into each and every theme, only the main one.
 * Edit: I forgot to mention that the function works great for the template that
   is in the theme folder. Can you give any guidance for the folder parallel to 
   the theme folder (and maybe not on the same level)?
 *  [esmi](https://wordpress.org/support/users/esmi/)
 * (@esmi)
 * [13 years, 7 months ago](https://wordpress.org/support/topic/using-a-custom-page-template-based-on-php-switch-statement/#post-3142268)
 * > does that search until it finds the “store-template” in any directory or folder
   > in wp-content?
 * No. It will only look in your theme’s top level folder.
 *  [wpismypuppet](https://wordpress.org/support/users/wordpressismypuppet/)
 * (@wordpressismypuppet)
 * [13 years, 7 months ago](https://wordpress.org/support/topic/using-a-custom-page-template-based-on-php-switch-statement/#post-3142269)
 * Well, if each of your themes are child themes off the main one, this would work
   fine. The function will check the current child theme first, then check it’s 
   parent. It does not search the entire wp-content folder.
 * If you need that functionality, then you’ll want to use the absolute path. However
   the WordPress ABSPATH variable changes with each page. And adding that with a../
   will cause problems.
 * To get the absolute path of your website, you’ll want to use the PHP variable
   [$_SERVER[‘DOCUMENT_ROOT’]](http://php.net/manual/en/reserved.variables.server.php)
   and then add in the rest. So something like this:
 * `<?php include($_SERVER['DOCUMENT_ROOT'] . "/wp-content/uploads/stores/store-
   template.php") ; ?>`
 *  Thread Starter [kirkward](https://wordpress.org/support/users/kirkward/)
 * (@kirkward)
 * [13 years, 7 months ago](https://wordpress.org/support/topic/using-a-custom-page-template-based-on-php-switch-statement/#post-3142272)
 * How can I include and use templates that are not in the theme folder?
 * I tried the full path in the get-template_part function, alas, to no avail. i.
   e.
 *     ```
       get_template_part('full_url_to_template');
       ```
   
 * didn’t work.
 *  Thread Starter [kirkward](https://wordpress.org/support/users/kirkward/)
 * (@kirkward)
 * [13 years, 7 months ago](https://wordpress.org/support/topic/using-a-custom-page-template-based-on-php-switch-statement/#post-3142275)
 * Sorry esmi and wpismypuppet. Seems it takes me longer to think and try than it
   does for you guys to answer.
 * I’ll be back in a moment.
 *  Thread Starter [kirkward](https://wordpress.org/support/users/kirkward/)
 * (@kirkward)
 * [13 years, 7 months ago](https://wordpress.org/support/topic/using-a-custom-page-template-based-on-php-switch-statement/#post-3142283)
 * That Is So Cool!
 * Thanks.
 *     ```
       <?php include($_SERVER['DOCUMENT_ROOT'] . "/wp-content/uploads/stores/store-template.php") ; ?>
       ```
   
 * worked for my store templates and
 *     ```
       get_template_part('single') ;
       ```
   
 * keeps the managers out of trouble!
 * Thanks, thanks, thanks.
 *  [wpismypuppet](https://wordpress.org/support/users/wordpressismypuppet/)
 * (@wordpressismypuppet)
 * [13 years, 7 months ago](https://wordpress.org/support/topic/using-a-custom-page-template-based-on-php-switch-statement/#post-3142293)
 * You are welcome… glad to be of help.
 *  Thread Starter [kirkward](https://wordpress.org/support/users/kirkward/)
 * (@kirkward)
 * [13 years, 7 months ago](https://wordpress.org/support/topic/using-a-custom-page-template-based-on-php-switch-statement/#post-3142308)
 * I know this thread is marked resolved, and it is. I didn’t know where else to
   make this comment.
 * I’ll be 71 y.o. next month, and I started with WordPress because I couldn’t understand
   the coding folks were using to build my websites. As time goes on, and I want
   to do more and more, I keep getting amazed at how powerful WordPress is, and 
   how it can be tweaked with small snippets of code. A person doesn’t have to know
   a whole language (while I’m sure it does help) in order to accomplish a lot with
   WP. Keep after the core coders to keep innovating. It’s really cool to watch 
   this thing develop.
 * Kirk

Viewing 9 replies - 1 through 9 (of 9 total)

The topic ‘Using A Custom Page Template Based On PHP Switch Statement’ is closed
to new replies.

## Tags

 * [ABSPATH](https://wordpress.org/support/topic-tag/abspath/)
 * [php](https://wordpress.org/support/topic-tag/php/)
 * [switch](https://wordpress.org/support/topic-tag/switch/)
 * [template](https://wordpress.org/support/topic-tag/template/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 9 replies
 * 3 participants
 * Last reply from: [kirkward](https://wordpress.org/support/users/kirkward/)
 * Last activity: [13 years, 7 months ago](https://wordpress.org/support/topic/using-a-custom-page-template-based-on-php-switch-statement/#post-3142308)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
