Sign up ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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 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 has been asked before and already has an answer. If those answers do not fully address your question, please ask a new 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 I moved my answer over there and recommend this question be deleted. – Doug Aug 5 '11 at 19:20

1 Answer 1

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.