为什么 PHP/WordPress 在这里回显空行?

发布于 2024-12-14 03:29:41 字数 1517 浏览 10 评论 0原文

我有一个非常简单的循环用于网站上的存档页面:

<?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 的内容。看一眼页面源代码,

与其包含的
之间没有任何内容(甚至没有空格),但是

; 在页面上向下推一行,开发者控制台在其上方显示一个空文本节点:

A string上面的空间H2 元素

草率的空白是一回事;实际上影响我的布局的空白是另一个。我是否缺少某些东西可能会导致此问题?

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:

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

拥有 2024-12-21 03:29:41

您是否曾经使用记事本创建过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.

悲喜皆因你 2024-12-21 03:29:41

看起来你在 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; ?>

糖果控 2024-12-21 03:29:41
   <?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(); ?>

我认为你应该移动结束标签,它搞砸了你的标记

   <?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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文