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.

Just wondering if there's anything particularly wrong with putting theme elements like navigation menus, or even footer content or anything else that is going to be the same on every page inside the posts loop. (<?php while ( have_posts() ) : the_post() ?> )

Obviously for most themes, it makes sense for the posts to have their own div and to put the loop in there- But I'm just wondering if it is an actual limitation or not.

share|improve this question
    
You are mixing some concepts there. Check the documentation about The Loop. –  brasofilo Jan 7 '13 at 21:16
add comment

2 Answers

  1. If you do this on archive pages you will get a lot of duplication
  2. You can have trouble if one of those elements alters or overwrites $wp_query or $post or any number of other things

Keep your page structure clean and logical. What you are asking is something like "Is there any problem with putting my underwear on over my pants?" Well, you might get it to work, but it is odd, your underwear isn't really doings its job anymore, and your friends and family may start to mysteriously pass on diner invitations.

share|improve this answer
add comment

The only things that should go inside the Loop are those things that are meant to be output for each and every post output by the loop.

Does it make sense for the Site Title to be displayed for each and every post in the loop? Certainly not. Thus, it should be output outside the Loop.

Does it make sense for a Post Title to be displayed for each and every post in the loop? Well, yes. That's why <?php the_title(); ?> is called inside the Loop, rather than outside.

Does it make sense for a navigation menu to displayed for each and every post in the loop? Not generally, no (though there are rare edge cases for just about everything).

Does it make sense for site footer content to be displayed for each and every post in the loop? I would say definitely not, and by doing so, you're going to break the HTML markup of the rendered page.

Edit

In re: this comment:

...and say for example you want to put meta post information inside the nav bar?

You could simply make use of the is_single() conditional template tag, inside your navbar.

<?php
// Inside your navbar
if ( is_single() ) {
    // This is a single blog post page; display something
    global $post;
    $post_meta = get_post_meta( $post->ID, ... ); // etc.
}
?>

This works because, in the context of a single post view, you essentially have access to a single $post object, even outside of the Loop.

share|improve this answer
    
OK, thanks. But how about if the theme in question only ever displays one post at a time, and say for example you want to put meta post information inside the nav bar? –  sanjaypoyzer Jan 7 '13 at 15:43
    
"But how about if the theme in question only ever displays one post at a time..." - How can you guarantee that? If it's a publicly distributed Theme, you can't. See updated answer for the second part of your question. –  Chip Bennett Jan 7 '13 at 16:07
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.