Why is PHP/WordPress echoing a blank line here?

I have a very simple loop for an archive page at a website:

<?php get_header(); ?>
<?php if(have_posts()): ?>
<div id="thumbs-container">
<?php while(have_posts()): the_post(); if(has_post_thumbnail()): ?>
    <div <?php post_class('thumb'); ?>>
        <a href="<?php the_permalink(); ?>">
        <?php the_post_thumbnail('full',array('alt'=>get_the_title(),'title'=>null)); ?>
        <div class="thumb-desc">
            <h2><?php the_title(); ?></h2>
            <p><?php echo get_post_meta($post->ID,'Description',true); ?></p>
        </div>
        </a>
    </div>
<?php endif; endwhile; ?>
</div>
<?php else: get_template_part('no-results'); endif; ?> // <--problem here?
<?php get_footer(); ?>

And here is the no-results template in its entirety:

Read More
<h2>Nothing here yet.</h2>
<p>Please <a href="<?php bloginfo('url'); ?>/">return to the home page</a>.</p>

Since the archive is currently empty, the contents of no-results.php are shown, as expected. A glance at the page source shows nothing (not even white space) between the <h2> and its containing <div>, however the <h2> is pushed down one line on the page and the Developer Console shows me an empty text node above it:

A string of spaces above an H2 element

Sloppy whitespace is one thing; whitespace that actually affects my layout is another. Is there something I’m missing which might be causing this?

Related posts

Leave a Reply

3 comments

  1. Did you by any chance use Notepad to create your no-results template?

    I would guess that your template starts with a UTF-8 Byte order mark. Notepad inserts it by default when you save a file with the UTF-8 encoding. Some other text editors also do that, but usually have an option to turn it off.

    Use an editor that doesn’t insert a BOM, or, considering your template doesn’t contain any non-ASCII characters anyway, simply save it as ASCII/ANSI.

  2. Seems like you are close thing if before the while loop?

    <?php endif; endwhile; ?> Seems wrong because you open the if fist and the while second so you should close them in most recent first order e.g.
    <?php endwhile; endif; ?>

  3.    <?php get_header(); ?>
        <?php if(have_posts()): ?>
        <div id="thumbs-container"><-- this will echoed if there is content
        <?php while(have_posts()): the_post(); if(has_post_thumbnail()): ?>
            <div <?php post_class('thumb'); ?>>
                <a href="<?php the_permalink(); ?>">
                <?php the_post_thumbnail('full',array('alt'=>get_the_title(),'title'=>null)); ?>
                <div class="thumb-desc">
                    <h2><?php the_title(); ?></h2>
                    <p><?php echo get_post_meta($post->ID,'Description',true); ?></p>
                </div>
                </a>
            </div> <-- here should be the closing tag of thumbs-container
        <?php endif; endwhile; ?>
        </div> <-- the closing tag will be always echoed better moved up
        <?php else: get_template_part('no-results'); endif; ?> // <--problem here?
        <?php get_footer(); ?>
    

    i think you should move the closing tag wich screwed your markup