Title: Get_Template_Part variable
Last modified: May 2, 2022

---

# Get_Template_Part variable

 *  [alphamike2](https://wordpress.org/support/users/alphamike2/)
 * (@alphamike2)
 * [4 years, 1 month ago](https://wordpress.org/support/topic/get_template_part-variable/)
 * Dear all,
 * It’s for sure a very basic question to most of you but I have three templates(
   EN,FR,DE). What I want is that the language of a select article is displayed 
   here :
 * get_template_part( ‘template-parts/content’, **here** );
 * I’ve added the following in each of my three dedicated templates : <?php $var
   = ‘en’; ?>
 * What I want now is to grab that EN (for example) and add it automatically here,
   I tried to add the following :
    get_template_part( ‘template-parts/content’, **
   $var** );
 * But that doesn’t work…
 * Any idea ?
 * Many thanks !!
    -  This topic was modified 4 years, 1 month ago by [alphamike2](https://wordpress.org/support/users/alphamike2/).

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

 *  [catacaustic](https://wordpress.org/support/users/catacaustic/)
 * (@catacaustic)
 * [4 years, 1 month ago](https://wordpress.org/support/topic/get_template_part-variable/#post-15610460)
 * What you’ve got is right, so it _should_ work, assuming that nothing else is 
   altering things in between.
 * First thing is to do some basic de-bugging. Try adding this to see what happens:
 *     ```
       <?php
           echo "<p>Var value: '" . $var . "'</p>";
           get_template_part( ‘template-parts/content’, $var );
       ?>
       ```
   
 * That will show you what the value is for $var when it’s used. If it’s not what
   you expect, then you’ll ned to figure out where it changes.
 *  Thread Starter [alphamike2](https://wordpress.org/support/users/alphamike2/)
 * (@alphamike2)
 * [4 years, 1 month ago](https://wordpress.org/support/topic/get_template_part-variable/#post-15610515)
 * Thanks for your message.
 * It unfortunately doesn’t work… this might be about my file structure don’t you
   think ?
 * theme:
    single.php (where my get_template_part is) – template-parts – – content-
   de.php (where my <?php $var = ‘de’; ?> is) – – content-en.php (where my <?php
   $var = ‘en’; ?> is) – – content-fr.php (where my <?php $var = ‘fr’; ?> is)
 * I had to add the following so that the single.php recognizes the location of 
   the variable :
 *     ```
       <?php 
   
       include 'template-parts/content-fr.php';
       include 'template-parts/content-de.php';
       include 'template-parts/content-en.php';
   
       ?>
       ```
   
 * Is that correct ?
 * Thanks !!
 *  Thread Starter [alphamike2](https://wordpress.org/support/users/alphamike2/)
 * (@alphamike2)
 * [4 years, 1 month ago](https://wordpress.org/support/topic/get_template_part-variable/#post-15610516)
 * Also, the :
 * <?php
    echo “<p>Var value: ‘” . $var . “‘</p>”; get_template_part( ‘template-
   parts/content’, $var ); ?>
 * is giving me the same result : “EN” which is not correct…
 *  [catacaustic](https://wordpress.org/support/users/catacaustic/)
 * (@catacaustic)
 * [4 years, 1 month ago](https://wordpress.org/support/topic/get_template_part-variable/#post-15610524)
 * > is giving me the same result : “EN” which is not correct…
 * So that’s what I said before. You need to figure out why that’s not correct. 
   Until you do that, it won’t matter what else you do.
 * With your folder strucutre, that looks correct, so that’s not the issue.
 * Where are you actually setting the value for $var for each different template?
   You haven’t shown that code yet, and if that’s in the wrong place they it won’t
   work. If $var is global, it needs to be declared as global where it’s set as 
   well as in the temlate, and you need to be sure that you are setting it before
   you are requesting the template file.
 *  Thread Starter [alphamike2](https://wordpress.org/support/users/alphamike2/)
 * (@alphamike2)
 * [4 years, 1 month ago](https://wordpress.org/support/topic/get_template_part-variable/#post-15610529)
 * Alright ! The $var is now giving me the true result !
 * However, the page is displayed twice (one under the other), the first is “wrong”
   as it’s always english, but the last is true according to the language selected.
 * Do you think that this may be the cause ? :
 *     ```
       <?php 
   
       include 'template-parts/content-en.php';
   
       ?>
   
       <?php
           if( have_posts() ){
               while( have_posts() ){
                   the_post();
   
                   get_template_part( 'template-parts/content', $var );
               }
           }
       ?>
       ```
   
 * (that’s my full single.php page)
 *  [catacaustic](https://wordpress.org/support/users/catacaustic/)
 * (@catacaustic)
 * [4 years, 1 month ago](https://wordpress.org/support/topic/get_template_part-variable/#post-15610569)
 * There’s no need to use include() at the start of your code. That’s what’s bringing
   in the first incorrect set of content.
 *  Thread Starter [alphamike2](https://wordpress.org/support/users/alphamike2/)
 * (@alphamike2)
 * [4 years, 1 month ago](https://wordpress.org/support/topic/get_template_part-variable/#post-15610571)
 * Thanks but I’m then getting the following error message :
 * Warning: Undefined variable $var in /Applications/MAMP/htdocs/wp/wp-content/themes/
   custom/single.php on line 6
 *  [catacaustic](https://wordpress.org/support/users/catacaustic/)
 * (@catacaustic)
 * [4 years, 1 month ago](https://wordpress.org/support/topic/get_template_part-variable/#post-15610575)
 * So where are you setting the value for $var? It seems like that’s set inside 
   the template file, and that’s very wrong. That value needs to be set somewhere
   that it can be accessed by your script.
 *  Thread Starter [alphamike2](https://wordpress.org/support/users/alphamike2/)
 * (@alphamike2)
 * [4 years, 1 month ago](https://wordpress.org/support/topic/get_template_part-variable/#post-15610576)
 * That’s what I did, I had it set in my template file. I’m now confused on where
   I should have it…
 *  [catacaustic](https://wordpress.org/support/users/catacaustic/)
 * (@catacaustic)
 * [4 years, 1 month ago](https://wordpress.org/support/topic/get_template_part-variable/#post-15610585)
 * You can’t set a value in a file that’s included _after_ you need to use that 
   value.
 * You need to set the value in your main template file, not in the part files. 
   As an example…
 *     ```
       <?php
           $var = {SET HERE};
   
           if( have_posts() ){
               while( have_posts() ){
                   the_post();
   
                   get_template_part( 'template-parts/content', $var );
               }
           }
       ?>
       ```
   
 *  Thread Starter [alphamike2](https://wordpress.org/support/users/alphamike2/)
 * (@alphamike2)
 * [4 years, 1 month ago](https://wordpress.org/support/topic/get_template_part-variable/#post-15610589)
 * Perfect ! Many thanks, I’ll correct all of that !!

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

The topic ‘Get_Template_Part variable’ is closed to new replies.

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 11 replies
 * 2 participants
 * Last reply from: [alphamike2](https://wordpress.org/support/users/alphamike2/)
 * Last activity: [4 years, 1 month ago](https://wordpress.org/support/topic/get_template_part-variable/#post-15610589)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
