Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I want to get the template in the theme which being in use of the current edited post(_type)/Page.

Lets say im editing a single Post, Then i want to retrive that it using the template single.php

And if im editing a Custom Post Type that have its own single-{post_type}.php i want to retrive that.

How can i archive that? are there any global variables that store this information?

I know that pages saves it as meta_data.

I dont have mutch to go with but i have been looking att get_query_template and locate_template but i need something in the right direction.

Thanks.

share|improve this question

2 Answers 2

up vote 1 down vote accepted

Basically this isn't available in admin. You can take a look at my profile, go to my github page and grab the "current admin info" plugin. But this won't help you much.

This would show you the template name:

get_page_template_slug( get_queried_object_id() );

On the front end you can use the following plugin, that will display that info (it's from another question).

<?php
/** Plugin Name: (#10537) »kaiser« Get Template file name */

if ( ! class_exists( 'wpse10537_template_name' ) )
{
    add_action( 'plugins_loaded', array( 'wpse10537_template_name', 'init' ) );

class wpse10537_template_name
{
    protected static $instance;

    public $stack;

    public static function init()
    {
        is_null( self :: $instance ) AND self :: $instance = new self;
        return self :: $instance;
    }

    public function __construct()
    {
        if ( is_admin() )
            return;

        add_action( 'wp', array( $this, 'is_parent_template' ), 0 );
        add_action( 'wp', array( $this, 'get_template_file' ) );
        add_action( 'template_include', array( $this, 'get_template_name' ) );
        add_action( 'wp_footer', array( $this, 'get_template_name' ), 100 );
    }

    public function get_template_name( $file )
    {
        if ( 'template_include' === current_filter() )
        {
            $this->to_stack(
                 "Template file"
                ,basename( $file )
            );
            return $file;
        }

        // Return static var on echo call outside of filter
        if (
            current_user_can( 'manage_options' )
            AND defined( 'WP_DEBUG' )
            AND WP_DEBUG 
            )
        {
            return printf(
                 '<div><p style="text-align:center;">%s</p></div>'
                ,implode( " &ndash; ", $this->stack )
            );
        }
    }

    public function get_template_file()
    {
        if ( ! is_post_type_hierarchical( get_post_type() ) )
            return;

        $slug = get_page_template_slug( get_queried_object_id() );
        if ( ! strstr( $slug, "/" ) )
            return $this->to_stack( "Template", $slug );

        // Subdirectory info
        $this->to_stack(
             "Subdirectory"
            ,strstr( $slug, "/", true )
        );

        $this->to_stack(
             "Template (in subdirectory)"
            ,str_replace( "/", "", strstr( $slug, "/" ) )
        );
    }

    public function is_parent_template()
    {
        if ( ! is_null( wp_get_theme()->parent ) )
            return $this->to_stack( 'from parent theme' );

        $this->to_stack( 'from current/child theme' );
    }

    public function to_stack( $part, $item = '' )
    {
        $this->stack[] = "{$part}: {$item}";
    }
} // END Class wpse10537_template_name

} // endif;

If all this doesn't help as starting point, then please take a look at all the related answers.

share|improve this answer
    
Thank you Kaiser! –  Pontus Abrahamsson Dec 12 '12 at 8:22
    
@PontusAbrahamsson Could you update my answer with what you finally used (for later readers)? Thanks. –  kaiser Dec 12 '12 at 12:01
    
@PontusAbrahamsson Mods denied the edit. I'll move it to another answer, later on correct it. –  kaiser Dec 13 '12 at 19:19

This answer was originally an edit of the OP to my other answer. I'll correct the code later on.

So i ended up with a pretty simple solution. This answer did get me in the right direction.

I don't now if this is the best way to do it but it fills my requirements:

if ( $pagenow == 'post.php' )
{

    $template = get_post_meta( $post->ID, '_wp_page_template', true );

    // Check page-templates, if not set -> page.php
    if ( $post->post_type == 'page' && ( empty( $template ) || 'default' == $template ) )
    {
        $template = 'page.php';
    }
    elseif( empty( $template ) )
    {
        // Check single-templates, single-{post_type}.php -> single.php order
        if ( file_exists( TEMPLATEPATH .'/single-'. $post->post_type .'.php' ) ) {
            $template = 'single-'. $post->post_type .'.php';
        }
        elseif ( ( $post->post_type == 'attachment' || $post->post_type == 'page' ) && file_exists( TEMPLATEPATH .'/'. $post->post_type .'.php' ) )
        {
            $template = $post->post_type .'.php';
        }
        elseif( $post->post_type == 'attachment' )
        {
            $template = '';
        }
        else
        {
            $template = 'single.php';
        }
    }

    // Read the file
    $file = implode( '', file( TEMPLATEPATH .'/' . $template ) );

    // Get the tags in the template, (Blocks Areas: Left, Right)
    if ( preg_match_all( '|Block Areas:(.*)$|mi', $file, $name ) )
    {
        $defined_areas = explode( ',', strtolower( $name[1][0] ) );
        $defined_areas = array_map( 'trim', $defined_areas);
    }
} 
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.