WordPress.org

Ready to get started?Download WordPress

Forums

get name of page template on a page (14 posts)

  1. NicholasReed
    Member
    Posted 5 years ago #

    Hello, This would not be an issue in the main code of the page template, however, I want to find out what the name of the page template is inside header.php which is called from within the page template.

    This is so I can apply an appropriate id to the body tag.

    Thanks.

  2. get_vishu_here
    Member
    Posted 5 years ago #

    I was having the same problem then I figured out an alternative way.
    In your page template right after the page template name definition which is usually a comment. use this code:

    update_option('current_page_template','write the page template name here');

    once you do that wordpress saves this value in it's database.

    Now next thing you already know just to get this value to use it as your body id, in my case I used class.

    Many wordpress developers know this but I'm writing here just for convenience.
    Use the following code in header.php to know the current template:

    <body id="<?php echo get_option('current_page_template'); ?>">

    I'd be happy if this helps.

  3. NicholasReed
    Member
    Posted 5 years ago #

    I think this will work great. Thanks dude.

  4. Peter Butler
    Member
    Posted 5 years ago #

    I hadn't ever thought of that, but it makes perfect sense. Thanks.

  5. masabi
    Member
    Posted 5 years ago #

    Hi,

    Just FYI this is an extremely dangerous way to achieve that!

    You are effectively performing a database INSERT/UPDATE at the top of the page, and then a SELECT back again in the header. Not only is this an extremely slow and inefficient way to build a page, it is also in no way thread-safe.

    For example - if two people ask for different pages at the same time, it is quite possible that page 1 will write it's name to the database, then the server will suspend processing of that thread and begin processing page 2, write that name ot the database over the top of page 1's, then pause and continue building page 1 - but page 1 will now retrieve page 2's name from the database. There is no way to predict what will actually occur, and it will only get more weird and wrong the heavier the load on the server.

    Cheers,

    Tom.

  6. underblob
    Member
    Posted 4 years ago #

    I am not sure if this will solve your problem but I found you can get boolean on the template filename via
    is_page_template ( 'template_filename.php' )
    http://codex.wordpress.org/Function_Reference/is_page_template

  7. underblob
    Member
    Posted 4 years ago #

    Another way that I was solving <body> and other formatting for templates is to create a CSS file specific for the template and then insert it dynamically into the <head> from the template. In functions.php of your theme, add a function to spit out a <link> to the CSS:

    function mytemplate_head () {
      echo '<link rel="stylesheet" type="text/css" href="'.get_bloginfo('template_directory').'/mytemplate.css" />'
    }

    Then in the top of your template file before get_header();, add this line of code:

    add_action ( 'wp_head', 'mytemplate_head' );

    Since the mytemplate.css file will be loaded after your theme CSS, you can over-ride your theme CSS for <body> if you use !important flag after your property declarations:

    body {
      background: #000 !important;
    }

    http://codex.wordpress.org/Function_Reference/add_action
    http://www.w3.org/TR/CSS2/cascade.html#important-rules

  8. Justin Tadlock
    Member
    Posted 4 years ago #

    This will grab the file name of the template:

    global $wp_query;
    
    $template_name = get_post_meta( $wp_query->post->ID, '_wp_page_template', true );

    You can use str_replace() to strip the .php from the end.

  9. jasongeiger
    Member
    Posted 4 years ago #

    greenshady, that works great.

    I'm wonder how to extend it to include the default templates like: archive, single, search, etc.

  10. Eric P
    Member
    Posted 4 years ago #

    print_r( debug_backtrace() )

    will return an array of all physical files called by the script. Could write a plugin, or a function to show just the template files based on a condition of "/themes/" of the found files...

  11. Eric P
    Member
    Posted 4 years ago #

    I cooked this up ... It may be useful as a plugin? Any interest as a plugin, give me a shout ... eric pecoraro _at_ shepard dot com

    It involves some overhead, so it would obviously be used for development only. Not tested thoroughly, but it works on my system.

    <?php
    // this can live in /themes/mytheme/functions.php, or maybe as a dev plugin?
    function get_template_name () {
    	foreach ( debug_backtrace() as $called_file ) {
    		foreach ( $called_file as $index ) {
    			if ( !is_array($index[0]) AND strstr($index[0],'/themes/') AND !strstr($index[0],'footer.php') ) {
    				$template_file = $index[0] ;
    			}
    		}
    	}
    	$template_contents = file_get_contents($template_file) ;
    	preg_match_all("(Template Name:(.*)\n)siU",$template_contents,$template_name);
    	$template_name = trim($template_name[1][0]);
    	if ( !$template_name ) { $template_name = '(default)' ; }
    	$template_file = array_pop(explode('/themes/', basename($template_file)));
    	return $template_file . ' > '. $template_name ;
    }
    ?>

    Useage (in /themes/mytheme/footer.php would work)

    <?php
    if (is_user_logged_in()) {
    	echo ' | '. get_template_name() ;
    }
    ?>
  12. scottnelle
    Member
    Posted 4 years ago #

    here's the super simple solution that I use:

    at the top of each page template that needs a special id:

    <?php
    $tpl_body_id = 'portfolio';
    ?>

    in header.php:

    <?php
    global $tpl_body_id;
    if (!$tpl_body_id) { $tpl_body_id = 'default'; }
    ?>
    </head>
    <body id="<?php echo $tpl_body_id; ?>">

    If it's loaded a template where $tpl_body_id has been set, it will use that value for the body id. Otherwise it will use 'default'

  13. xiatica
    Member
    Posted 4 years ago #

    Doesn't just the

    <body <?php body_class(); ?>>

    print it out anyway? (Except on the posts page for me, which scottnelle's lovely solution doesn't work on either, for me).

  14. nosro
    Member
    Posted 4 years ago #

    The body_class() was added in WP 2.9, I think.

    To have complete control over the body class without using the prebuilt ones you'd need to use something like scottnelle's solution.

Topic Closed

This topic has been closed to new replies.

About this Topic