Post Pic

How to check if a page template is active

Conditionnal tags are very useful when you need to know if you’re on a post, page, homepage, etc. But how to know if you’re are on a specific page template? Nothing hard, just use the following recipe.

Paste the following on your functions.php file:

function is_pagetemplate_active($pagetemplate = '') {
	global $wpdb;
	$sql = "select meta_key from $wpdb->postmeta where meta_key like '_wp_page_template' and meta_value like '" . $pagetemplate . "'";
	$result = $wpdb->query($sql);
	if ($result) {
		return TRUE;
	} else {
		return FALSE;
	}
}

Once done, you can call the function like this:

if (is_pagetemplate_active("Archives")) {
  // The "Archives" page template is active
} else {
  // The "Archives" page template is NOT active
}

Credits goes to Wupper Piraten for this cool recipe!

Leave a Comment

* Name, Email, Comment are Required