WordPress.org

Ready to get started?Download WordPress

Forums

Checking for the existence of an active page template (6 posts)

  1. Ian Stewart
    Theme Wrangler
    Posted 4 years ago #

    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.

  2. nbseminary
    Member
    Posted 4 years ago #

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

  3. nbseminary
    Member
    Posted 4 years ago #

    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?

  4. MichaelH
    Member
    Posted 4 years ago #

    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); ?>
  5. nbseminary
    Member
    Posted 4 years ago #

    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";
    	}
    }
  6. MichaelH
    Member
    Posted 4 years ago #

    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";
    }

Topic Closed

This topic has been closed to new replies.

About this Topic