WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Calling whatever variable is assigned to a Page Template (8 posts)

  1. Clicknathan
    Member
    Posted 7 years ago #

    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:

    <$php
    /*
    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?

  2. rob0210
    Member
    Posted 6 years ago #

    Hi nathan,

    i found this on this page

    http://codex.wordpress.org/Conditional_Tags

    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.

  3. dave_t
    Member
    Posted 6 years ago #

    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?

  4. Michael Fields
    Themer
    Posted 6 years ago #

    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():
      $post->my_custom_var='stuff';
      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');
  5. dave_t
    Member
    Posted 6 years ago #

    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.

  6. dave_t
    Member
    Posted 6 years ago #

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

  7. Clicknathan
    Member
    Posted 6 years ago #

    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:

    is_page_template('filename.php')

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

  8. Clicknathan
    Member
    Posted 6 years ago #

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

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.