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 have a page.php and single.php template that are 100% identical. Yet, when I render page.php the div don't get nested in the same way as single.php and the layout gets broken.

I've even diff'd the output of the two pages and, with the exception of the meta and non visible elements, they are also 100% identical.

Following is the code of both templates:

<?php get_header(); ?>

<?php 
  if ( have_posts() ) while ( have_posts() ) : the_post(); 
  $post_id = $post->ID;
?>

<div class='container span12' id='single-post'>

    <div class='span8 alignleft'>
      <div class='post single'>

        <h2 class='entry-title'><?php the_title(); ?></h2>

        <?php the_content(); ?>

        <?php comments_template( '', true ); ?>
        <?php endwhile; ?>
      </div> 
    </div>

    <div class='span4 alignright'>
      <div id='sidebar'>
        <?php get_sidebar(); ?>
      </div>
    </div>

</div><!--end container-->

<?php get_footer(); ?>

What should I check to fix this issue?

share|improve this question
    
Please post your actual code; otherwise, we have no way to help. –  Chip Bennett Nov 27 '12 at 14:33
    
You simply break your while loop at the middle... –  kaiser Nov 27 '12 at 14:51
    
kaiser, thanks for the answer but could you explain that? –  L. De Leo Nov 27 '12 at 15:05
2  
imagine you're cracking eggs into a bowl to make a cake. Instructions go as follows: "while (there are eggs in the box) { lift egg out of box } crack egg, dispose of shell" as you can see every egg gets lifted out of the box, but then gets dropped as you lift another egg, only the last egg is cracked open into the bowl. This si what you've done with your code –  Tom J Nowell Nov 27 '12 at 15:36
    
Hi Tom, I don't see that. The php code might not be nested nicely with the html but it did what I wanted (which was to print 1 post or page content). The only wrong thing I see is the lack of endif. Just thought php was able to do without. –  L. De Leo Nov 27 '12 at 15:49
show 4 more comments

2 Answers 2

up vote 5 down vote accepted

Edit

Based on this comment:

commenting out comments_template works.

The problem lies in the template markup in comments.php. The likely reason that you see it in some output and not others is likely because you've got comments enabled in one context, and not in the other. Try comparing single posts with comments enabled vs disabled, and you'll likely see the same thing happen.

Original

You've closed your loop in the wrong place. Your <?php endwhile; ?> is in the middle of your post-container markup. Also, you don't ever close your if clause.

I would modify your markup to be a bit more intuitive, and also close the loop properly:

<div class='container span12' id='single-post'>

    <div class='span8 alignleft'>

        <?php 
           if ( have_posts() ) while ( have_posts() ) : the_post(); 
           $post_id = $post->ID;
        ?>

        <div class='post single'>

            <h2 class='entry-title'><?php the_title(); ?></h2>

            <?php the_content(); ?>

            <?php comments_template( '', true ); ?>

        </div><!-- .post single -->

        <?php endwhile; endif; ?>

    </div><!-- .span8 alignleft -->

    <div class='span4 alignright'>
      <div id='sidebar'>
        <?php get_sidebar(); ?>
      </div>
    </div>

</div><!--end container-->

That should resolve any <div> nesting problems related to the Loop. If not, you'll need to provide a live link to the rendered output, so we can see what's happening.

share|improve this answer
    
Hi Chip, thanks for the answer. I fixed the code as you suggested but the problem persists. In page.php the .container and the .span4 are siblings while in the single.php the .span4 and .span8 are siblings (as I intended) and both are contained in the .container. –  L. De Leo Nov 27 '12 at 15:23
    
Unfortunately this code is still not published on the web so I can't show you. –  L. De Leo Nov 27 '12 at 15:23
    
What happens if you comment-out comments_template()? –  Chip Bennett Nov 27 '12 at 15:25
    
Thanks Chip, commenting out comments_template works. Strange that same call doesn't have the same effect on single.php no? –  L. De Leo Nov 27 '12 at 15:33
    
That means that the problem is in your template markup in comments.php. –  Chip Bennett Nov 27 '12 at 15:48
show 1 more comment

What should I check to fix this issue?

Your header.php. Your Widgets. Your sidebar code. Even your CSS could lead to the appearance that the <div>s don't nest.

Identical code does not produce different results in different places. Computers are reliable like that. You have broken markup. You need to find it. It could be just about anywhere in the page before the <div>s go bad, as reflected in the source of the page not necessarily in the rnedered, visual layout.

Use the W3C Validator. It will help. You can submit local files using the FireFox extension "Web Developer", or upload them manually.

share|improve this answer
    
Well it isn't the appearance that they don't nest: firebug shows a different nesting for the two pages. –  L. De Leo Nov 27 '12 at 15:17
    
That statement about "appearance" applied only to the CSS, not to everything else. –  s_ha_dum Nov 27 '12 at 15:33
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.