WordPress.org

Support

Support » Themes and Templates » Checking for the existence of an active page template

Checking for the existence of an active page template

  • Ian Stewart
    Theme Wrangler

    @iandstewart

    Wondering how one would go about checking for the existence of an active page template. Not “am I in a page template”. I know that is_page_template('template-name.php') does that. I want to check if template-name.php exists in wp_postmeta and run some code at init if does.

Viewing 5 replies - 1 through 5 (of 5 total)
  • nbseminary
    Member

    @nbseminary

    I have the same question. With is_page_template('template-name.php') you have to know the name of the template page. What if you want to evaluate whether or not a page is using any template? Is there a way to do that? Something like has_page_template($template-name).

    nbseminary
    Member

    @nbseminary

    In my searching I have come across mention of postmeta information _wp_page_template. Is there a way to access the table information for _wp_page_template to determine if and what template page is being used?

    Look at Function_Reference/get_post_meta, something like this if your page id is 76.

    $page_template = get_post_meta(76, '_wp_page_template', true); ?>
    nbseminary
    Member

    @nbseminary

    Thanks, Michael! I think I got it. I’m pretty new to php but hopefully learning. This is what I came up with.

    function check_page_template() {
    	if ( is_page() ) {
    		$page_template = get_post_meta(get_the_id(),'_wp_page_template',true);
    		echo "The page template is $page_template for this page";
    	} else {
    		echo "This is not a page";
    	}
    }

    Might want to change

    echo "The page template is $page_template for this page";

    to

    if {$page_template} {
    echo "The page template is $page_template for this page";
    }

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Checking for the existence of an active page template’ is closed to new replies.