为什么 PHP/WordPress 在这里回显空行?
我有一个非常简单的循环用于网站上的存档页面:
<?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(); ?>
这是no -results
模板完整:
<h2>Nothing here yet.</h2>
<p>Please <a href="<?php bloginfo('url'); ?>/">return to the home page</a>.</p>
由于存档当前为空,因此按预期显示 no-results.php
的内容。看一眼页面源代码,
与其包含的
之间没有任何内容(甚至没有空格),但是
;
在页面上向下推一行,开发者控制台在其上方显示一个空文本节点:
之间没有任何内容(甚至没有空格),但是
;
在页面上向下推一行,开发者控制台在其上方显示一个空文本节点:
草率的空白是一回事;实际上影响我的布局的空白是另一个。我是否缺少某些东西可能会导致此问题?
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:
<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:
Sloppy whitespace is one thing; whitespace that actually affects my layout is another. Is there something I'm missing which might be causing this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您是否曾经使用记事本创建过
no-results
模板?我猜您的模板以 UTF-8 字节顺序标记 开头。当您使用 UTF-8 编码保存文件时,记事本会默认插入它。其他一些文本编辑器也可以这样做,但通常有一个选项可以将其关闭。
使用不插入 BOM 的编辑器,或者考虑到您的模板不包含任何非 ASCII 字符,只需将其另存为 ASCII/ANSI。
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.
看起来你在 while 循环之前就已经很接近了?
似乎是错误的,因为您打开了 if 拳头和 while 第二个,所以您应该按照最近的第一顺序关闭它们,例如
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; ?>
我认为你应该移动结束标签,它搞砸了你的标记
i think you should move the closing tag wich screwed your markup