emersonthis
emersonthis

Reputation: 33378

How do I target a SPECIFIC template with is_page_template() in Wordpress 3+

I'm running Wordpress 3.2 and I need a conditional to test for a certain template. My template is a file called special_offer.php. The name of the template is "Special Offer". I've tried both of the following:

is_page_template('special_offer.php');
is_page_template('Special Offer');

Neither of these work! The documentation clearly says that the parameter should be a string with the name of the file so I'm not sure what's wrong. I know the function is at least partially working because if I don't include a parameter, it returns true for any pages using templates (as expected).

Upvotes: 1

Views: 4860

Answers (2)

emersonthis
emersonthis

Reputation: 33378

AHA! I solved this by adding wp_reset_query() just before the conditional.

I had already read warnings about many WP conditionals not working inside the loop. However, my conditional is NOT inside a loop. Turns out, it won't work even AFTER the loop, which is where mine was. So you you need to reset the query before you call it.

Upvotes: 2

agtb
agtb

Reputation: 477

It's worth double checking which file is being used according to the WordPress template hierarchy [image].

Try adding this magic constant to the template file you think is being called:

<?php echo(__FILE__); ?>

If you don't see a path and filename a different file is being used (refer to the template hierarchy diagram in the link above) or check which templates are available:

<?php print_r( get_page_templates() ); ?>

(^ looks like this has to be called from the admin interface e.g. in a plugin)

Also, the documentation seems to state that is_page_template() won't work in the Loop without calling another function first.

Upvotes: 0

Related Questions