Support » Themes and Templates » Calling whatever variable is assigned to a Page Template

  • Resolved Clicknathan


    Hello all,

    I’m trying to give each “section” of my blog a different header image. My plan was to drop something like $section = ‘home’ into the very top of my page_home.php Page Template, and then using an if/than statement to say if $section = ‘home’ show this images, and so on for each different page.

    But for some reason, by the time the page loads, it isn’t recognizing the variable anymore.

    So I’m wondering if anyone knows how I can find out the variable that WP sets to determine which Page Template it’s using.

    For example:

    Template Name: <strong>Homepage</strong>

    Is WP setting a variable for homepage here and if so, can I call that variable from another place in my code?

Viewing 7 replies - 1 through 7 (of 7 total)
  • Hi nathan,

    i found this on this page

    It uses the is_page_template…Im not sure if this only works in the loop though or whether it works in the header? And as far as I can see its new to 2.5.

    hey guys
    I’m trying to do something similar… nathan, are you calling your header (get_header) before or after you set your variable? Is it a case of making it global (eg global $section = '')? (Note: I’m no php-er, just a designer struggling with the same issue by trial and error.)
    I’ve been trying to use the is_page_template() conditional, but can’t seem to get it to work. Has anyone successfully used this? Does anyone know for sure it’s been included in 2.5?

    I too have struggled with this is the past, I have found two solutions:

    1. Store the variable in the $post object in your-template.php before your call to get_header():
      It is then possible to access $post->my_custom_var within header.php
    2. Don’t use get_header(), get_footer() or get_sidebar(). I stead use the following code: include_once (TEMPLATEPATH . '/header.php');

    ok, i’ve got is_page_template() working – you need to pass it the file-name, not the template name you assign within that file (contrary to the documentation on conditionals). I noticed you guys have got another thread going here, so will reply there too, in case it helps.

    mfields, reckon you could scoot on over to the other thread these guys have got going and post your advice there? it seems to be the more current thread… i’m new here, so not sure about the etiquette of dual threads etc, but hey, if it helps anyone…

    While mfields advice may work, with 2.5 and is_page_template() you can do this now.

    As dave_t stated, if you want to call a specific page template you need to use this formatting:


    I’ve confirmed this and it’s also been discussed here.

    I’ve updated the documentation as well to reflect this.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Calling whatever variable is assigned to a Page Template’ is closed to new replies.