WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Possible Duplicate:
Wordpress - Programmatically change post templates?

I need to change the template on several hundred pages on my site. Does anyone know if there is a command to do it programmatically? See the following code snippet; I'd like to replace my comment inside the loop with a function that will change the template of the current page:

query = new WP_Query($args);
while ( $query->have_posts() ) : $the_query->the_post();
  if ( has_tag('tag-slug') )
     /* CHANGE TEMPLATE OF $post */
share|improve this question

migrated from stackoverflow.com Aug 5 '11 at 13:30

This question came from our site for professional and enthusiast programmers.

marked as duplicate by Rarst Aug 5 '11 at 19:32

This question was marked as an exact duplicate of an existing question.

Watch out for the typo in your question. "$the_query->the_post" should be "$query->the_post". – Doug Aug 5 '11 at 19:12
Just noticed this question is a dup of wordpress.stackexchange.com/questions/24924 I moved my answer over there and recommend this question be deleted. – Doug Aug 5 '11 at 19:20

Try this:

while ( $query->have_posts() ) : $query->the_post();
  if ( has_tag('tag-slug') )
     get_template_part( 'content', 'tag-slug' );
     get_template_part( 'content' );


You would then need to create content.php and content-tag-slug.php with the appropriate content.

You could also look into setting up Post Formats. A bit more work up front, but a nicer experience down the road.

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.