Is a certain page template active?

Yesterday Ian came up with a nice question:

Can you make sidebar registration dependent on a template being activ? Like, front page sidebars only registered if front-page.php in use?

He’s not talking ‚bout displaying a certain sidebar on a page that uses a certain page template. That’s too easy :-)

Here’s my first attempt to solve this problem using a new conditional tag:

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

Veröffentlicht von

Chris

I'm the 2nd WordPress Theme Developer behind the popular Thematic Theme Framework. I love to make good things even better.

2 Gedanken zu „Is a certain page template active?“

  1. Chris, Thanks for this code! I was thinking about incorporating something similar to my own project, but have not gotten around to it yet. I found your function from twitter and tested it out. Works great for me on 2.9. Nice work!!!

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind markiert *