• Resolved tomkinsg

    (@tomkinsg)


    Hi, my wordpress journey continues….

    I am looking for how to do different things in my site based upon the page template seelcted when the page is created. I want to have some code that does something like..


    if(page_template = "hometemplate")
    {
    ... do home page stuff
    }
    else
    {
    ... do other stuff
    }

    I have looked at this page here and thought i was on to something

    http://wordpress.org/support/topic/35598

    but the $_GET(‘template’) always returns nothing 🙁

    thanks
    Glyn

Viewing 1 replies (of 1 total)
  • Thread Starter tomkinsg

    (@tomkinsg)

    .. well i persevered. I don’t know if this is the most elegant method but this is what i have done:

    1. I created a function called get_page_template_name() based on get_page_template() out of functions.php.

    it returns the file name (without the full path and without the .php extension) of the template in use. e.g. if i have templates called MyTemplate.php and YourTemplate.php – it will return MyTemplate or YourTemplate respectively.


    function get_page_template_name() {
    global $wp_query;
    $id = $wp_query->post->ID;
    $template = get_post_meta($id, '_wp_page_template', true);
    $template = substr($template,0,strlen($template)-4);
    return apply_filters('page_template', $template);
    }

    2. I created n new templates that i can select when creating pages. In this case MyTemplate.php and YourTemplate.php

    3. In my header.php that is used in all templates, I have this:


    <div id="header" <?php if (get_page_template_name()!="") echo "class=\"".get_page_template_name()."\"";?>>

    so it adds a class="MyTemplate" or a class="YourTemplate" or no class attribute at all.

    4. In my CSS, I have added some additional classes as needed:


    #header.MyTemplate {
    background-image:url(/wordpress/images/top/MyTemplate.jpg);
    }
    #header.YourTemplate {
    background-image:url(/wordpress/images/top/YourTemplate.jpg);
    }

    I hope this is useful for others. I would be interested to know if there is a more elegant way of doing ths.

    regards
    Glyn

Viewing 1 replies (of 1 total)

The topic ‘Getting the page template name’ is closed to new replies.