DISCLAIMER: this post is older than one year and may not be up to date with latest WordPress version.

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!

One Comment

  1. Thank you so much for this code 🙂

Leave a Comment

Your email address will not be published. Required fields are marked *