Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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 */
  }
endwhile;
wp_reset_postdata();
share|improve this question
    
Watch out for the typo in your question. "$the_query->the_post" should be "$query->the_post". –  Doug Aug 5 '11 at 19:18
add comment

3 Answers 3

I take it that you mean saving the pages with a new post-template to the DB? see if there's anything in the post object that refers to the page-template, if so craft a query that updates that.

Update:

Taken from the wp_insert_post documentation

NOTE: The page_template key was removed from the $post table and is now located in the wp_postmeta table. To set the page template of a page, use update_post_meta instead with a meta key of _wp_page_template.

So you can fetch the page template using get_post_meta, and update it with update_post_meta (Or set it with add_post_meta)

share|improve this answer
    
Yes, that's what I'm referring to. Do you know of a good source for documentation on the post object? –  The Mythical Bird Aug 4 '11 at 13:06
    
See updated answer :) –  Xeross Aug 4 '11 at 13:15
    
Thanks! I'd upvote you if I were able to. :) –  The Mythical Bird Aug 4 '11 at 13:26
add comment

What kinds of formatting are you referring to? If you want to change the formatting that can be changed by css. You can use post_class() which will add a class naming your tag in the container. Then you can style targeting that class.

Inside the loop:

<div <?php post_class()?> >
</div>

Also check the action pre_get_post; Using it with conditionals might help.

share|improve this answer
add comment

Try this:

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

endwhile;

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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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