Title: Template changes based on template code??
Last modified: August 20, 2016

---

# Template changes based on template code??

 *  Resolved [dorich](https://wordpress.org/support/users/dorich/)
 * (@dorich)
 * [13 years, 11 months ago](https://wordpress.org/support/topic/template-changes-based-on-template-code/)
 * I have a page that reads the variable ( a single letter) from the url. The page
   then displays custom posts assigned to that letter. (The letter is a custom field)
   
   The problem is that the template assigned to the page is not doing the final 
   rendering of the page. The template contains the following code:
 *     ```
       <?php $letter = $_GET['letter'];
   
       					if ($letter=="") $letter = 'A';?><?php /*If there is no value returned then the loop defaults to searching for items beginning with 'A'*/ ?>
       					<h1>Glossary Items Listed Under Letter "<?php echo $letter ?>"</h1> <?php /*This needs to be outside of the loop*/?>
       			<?php
       					$loop = new WP_Query( array( 'post_type' => 'glossary', 'meta_key'=> 'first-letter', 'meta_value' => $letter, 'posts_per_page' => 10 ) ); ?>
   
       					<?php while ( $loop->have_posts() ) : $loop->the_post();?>
                                       <div class="glossarybody"> 
   
       										<h2><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h2>
   
                                       </div>
   
                           <?php endwhile; ?>
       ```
   
 * When that code is on the page the page displays the correct results ( a list 
   of titles) but using the plugin ‘reveal templates’ I see that the page is being
   rendered with ‘page.php’.
    The correct template should be glossary-terms.php (
   I’ve tried using the filename terms.php in case the word glossary, the name of
   the custom post type involved, was causing a problem .) If I remove the above
   code so that I’m left with a more or less static page that has some html, header,
   footer and sidebar, then the page renders using the correct template. This suggests
   that something in the operation of WordPress is causing the template to change
   in the execution of the initial template.
 * I’d appreciate any pointers on what might be wrong with my setup and how to get
   the correct template to render the page.
    Thanks.

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

 *  [s_ha_dum](https://wordpress.org/support/users/apljdi/)
 * (@apljdi)
 * [13 years, 11 months ago](https://wordpress.org/support/topic/template-changes-based-on-template-code/#post-2889793)
 * The selection of the template page happens long before your code in the theme
   template file runs, and the template choice is dependent upon the main query,
   not your new WP_Query object. The question then is “How did you create this page?”
   Did you go to Pages->Add New? If so, the automatic template selection mechanisms
   are not going to work. As far as WordPress is concerned, this is just a page 
   like any other.
 *  Thread Starter [dorich](https://wordpress.org/support/users/dorich/)
 * (@dorich)
 * [13 years, 11 months ago](https://wordpress.org/support/topic/template-changes-based-on-template-code/#post-2889795)
 * @s_ha_dum
 * Thanks for your response.
 * In answer to your question.
    Page was created via the “add new” in the pages 
   section of the admin. Template is assigned as part of the page attributes Page
   is assigned to a parent in the page attributes
 * The calling link is hard coded.
 * My assumption is that since I’ve “hard coded” this then my selections should 
   override any automatic page/template selection.
 *  [s_ha_dum](https://wordpress.org/support/users/apljdi/)
 * (@apljdi)
 * [13 years, 11 months ago](https://wordpress.org/support/topic/template-changes-based-on-template-code/#post-2889801)
 * > My assumption is that since I’ve “hard coded” this then my selections should
   > override any automatic page/template selection.
 * Yes, that is right. And that is what I was going to tell you to do. You must 
   have something wrong in the configuration. Do you have a public URL for this 
   site and page? Also, it never hurts to reset the permalinks.
 *  Thread Starter [dorich](https://wordpress.org/support/users/dorich/)
 * (@dorich)
 * [13 years, 11 months ago](https://wordpress.org/support/topic/template-changes-based-on-template-code/#post-2889807)
 * @s_ha_dum
 * Thanks again.
 * I’ve got a hint of the problem but I have no idea what the cause might be.
 * The calling url was written as ” ………../terms/?letter=A”
    or “…../terms?letter
   =A”
 * Now if I add a trailing slash to the calling url I get the correct template, 
   thus
    “…../terms?letter=A/” Of course it doesn’t load any data since there are
   no matching terms for A/. My next course of exploration is to see if there is
   the possibility that a similar call is being from elsewhere (where elsewhere 
   means somewhere in the site that would use the page.php as opposed to the template
   I’m calling.)
 * Most of the time when I make changes I’m visiting the permalinks page under settings.
 * You can see this result by going to: book dot value-based-marketing.com and clicking
   on the library tab, then select Glossary Terms By First Letter.
    The letters 
   A and C display with the correct template but no data. Other letters such as 
   S will display the correct data but rendered by the incorrect template.
 * Scrolling to the bottom of the page will show the template that rendered the 
   page.
 *  [s_ha_dum](https://wordpress.org/support/users/apljdi/)
 * (@apljdi)
 * [13 years, 11 months ago](https://wordpress.org/support/topic/template-changes-based-on-template-code/#post-2889859)
 * The fact that you are using ‘terms’ could be problematic. See if it works with
   some other value.
 *  Thread Starter [dorich](https://wordpress.org/support/users/dorich/)
 * (@dorich)
 * [13 years, 11 months ago](https://wordpress.org/support/topic/template-changes-based-on-template-code/#post-2889872)
 * thanks.
 * I changed the slug to ‘unique’ and updated the calling link. Visited permalinks
   settings.
 * Still get the same result page is rendered with page.php
 *  [s_ha_dum](https://wordpress.org/support/users/apljdi/)
 * (@apljdi)
 * [13 years, 11 months ago](https://wordpress.org/support/topic/template-changes-based-on-template-code/#post-2889914)
 * Is there an URL I can visit?
 * Ok. Reset.
 * > Page is assigned to a parent in the page attributes
 * If you have a parent your URL needs to be “…/parent/terms/?letter=A” Is that 
   how you have it? I would assume that the template assignment should work with
   nested pages like that but I have never tried it. Have you tried without having
   a parent assigned?
 * The fact that your page is called ‘terms’ nags at me a little bit because generically
   categories and tags, and other taxonomies, are called ‘terms’. I don’t know it
   that is causing interference though.
 *  Thread Starter [dorich](https://wordpress.org/support/users/dorich/)
 * (@dorich)
 * [13 years, 11 months ago](https://wordpress.org/support/topic/template-changes-based-on-template-code/#post-2889915)
 * Thanks:
 * 1. I previously did some tests with removing the parent page but I went back 
   and did it again. Made the url in the form of example.com/terms/?letter=A , changed
   the calling link and visited the permalink settings . Still rendered with the
   wrong template.
    2. I changed the page slug from terms to “unique”, changed the
   calling link, visited permalink etc. Still rendered with page.php However, I’ll
   change the slug and page name to something less likely to cause a problem. 3.
   You can see this at book dot value-based-marketing.com/library/ Click on “find”,
   click on “Glossary list by letter” 4. I have established that if I change the
   contents of the custom field and change the calling link appropriately then it
   works. For example if I replace the letter A in the custom field with xyz and
   replace the variable on calling link xyz – then the page is rendered with the
   correct template.
 *  [s_ha_dum](https://wordpress.org/support/users/apljdi/)
 * (@apljdi)
 * [13 years, 11 months ago](https://wordpress.org/support/topic/template-changes-based-on-template-code/#post-2889916)
 * Why does your “A” link use “definitions” but the others use ‘terms’?
 * … value-based-marketing.com/library/glossary-start-here/definitions/?letter=A
 * vs.
 * …value-based-marketing.com/library/glossary-start-here/terms/?letter=F
 * In fact, the ‘definitions’ link is the only one that is rendering at all, and**
   not** with the wrong template. There is something wrong with that ‘reveal templates’
   plugin. Look at your ‘body’ class:
 * `<body class="page page-id-2383 page-child parent-pageid-2269 page-template page-
   template-glossary-terms-php singular two-column right-sidebar">`
 * See that “page-template-glossary-terms-php” part. That is the template being 
   used.
 * Other that “A” all of you your other links– the one with “terms” in the URL– 
   are 404s.
 * We’ve been chasing the wrong problem.
 *  Thread Starter [dorich](https://wordpress.org/support/users/dorich/)
 * (@dorich)
 * [13 years, 11 months ago](https://wordpress.org/support/topic/template-changes-based-on-template-code/#post-2889918)
 * > Why does your “A” link use “definitions” but the others use ‘terms’?
 * Apologies: I tested the change of slug on just one link. Then had a distraction
   before I got round to changing all the other links. All of the links now use “
   definitions”.
 * > See that “page-template-glossary-terms-php” part. That is the template being
   > used.
 * Ah, I didn’t know that I could see the template by looking at the body class.
 * So the conclusion is that the page is being rendered with the correct template
   but the plugin is failing. Indeed now I know where to look the plugin appears
   to be unnecessary.
 * > We’ve been chasing the wrong problem.
 * Yes, or at least I had us chasing the wrong problem.
    Thanks for all your help
   in getting me educated and the problem solved.
 *  [s_ha_dum](https://wordpress.org/support/users/apljdi/)
 * (@apljdi)
 * [13 years, 11 months ago](https://wordpress.org/support/topic/template-changes-based-on-template-code/#post-2889919)
 * > Ah, I didn’t know that I could see the template by looking at the body class.
 * If the theme uses the `body_class()` function you can.
 *  Thread Starter [dorich](https://wordpress.org/support/users/dorich/)
 * (@dorich)
 * [13 years, 11 months ago](https://wordpress.org/support/topic/template-changes-based-on-template-code/#post-2889922)
 * Noted, thanks.

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

The topic ‘Template changes based on template code??’ is closed to new replies.

## Tags

 * [custom post type](https://wordpress.org/support/topic-tag/custom-post-type/)
 * [page template](https://wordpress.org/support/topic-tag/page-template/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 12 replies
 * 2 participants
 * Last reply from: [dorich](https://wordpress.org/support/users/dorich/)
 * Last activity: [13 years, 11 months ago](https://wordpress.org/support/topic/template-changes-based-on-template-code/#post-2889922)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
