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;
	}
}
This entry was posted in WordPress and tagged , , . Bookmark the permalink. Post a comment or leave a trackback: Trackback-URL.

2 Kommentare

  1. Posted 19. Dezember 2009 at 16:39 | Permalink

    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!!!

  2. Posted 19. Dezember 2009 at 19:37 | Permalink

    Incredible! I saw this tweet appear and only hours later a viable solution!

    Thanks for this!

Einen Kommentar abgeben

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>