Wordpress:single.php 不显示 the_content()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8246386/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 04:16:01  来源:igfitidea点击:

Wordpress: single.php doesn't display the_content()

phpwordpresswordpress-theming

提问by Thomas Depole

I'm creating a custom Wordpress Theme and I can't seem to get the single.php template to work. Below is the code I have written. The title comes up but the content doesn't. Any Ideas why it isn't?

我正在创建一个自定义的 Wordpress 主题,但我似乎无法让 single.php 模板工作。下面是我写的代码。标题出现了,但内容没有。任何想法为什么不是?

<?php
/**
 * The Template for displaying all single posts.
 */

get_header(); ?>

<div id="content" role="main">
    <div <?php post_class() ?> id="post-<?php the_ID(); ?>">
        <h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
        <small><?php the_time('F jS, Y') ?> <!-- by <?php the_author() ?> --></small>

        <div class="entry">
            <?php the_content(); ?>
        </div>

        <p class="postmetadata"><?php the_tags('Tags: ', ', ', '<br />'); ?> Posted in <?php the_category(', ') ?> | <?php edit_post_link('Edit', '', ' | '); ?>  <?php comments_popup_link('No Comments ?', '1 Comment ?', '% Comments ?'); ?></p>
    </div>
</div><!-- #content -->

See here for a screenshot of the output:

有关输出的屏幕截图,请参见此处:

enter image description here

在此处输入图片说明

回答by lorem monkey

the_content()is not displaying because it has to be inside the The Loop- take a look at the docs here ?

the_content()未显示,因为它必须在The Loop 内-请查看此处的文档?

You need to change your code to this:

您需要将代码更改为:

if ( have_posts() ) : while ( have_posts() ) : the_post();
  the_content();
endwhile;
else:
  <p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
endif;

You can leave out the elseif you are always sure you have content to display :) Or just take look at the original single.phpwhere you can find The Loopalways surrounds the_content()

else如果您始终确定要显示的内容,则可以省略:) 或者只查看原始内容single.php,您可以在其中找到The Loopalways aroundthe_content()

edit:

编辑:

Here is the whole single.php you might wanna use/start with:

这是您可能想要使用/开始的整个 single.php:

<?php
/**
 * The Template for displaying all single posts.
 */

get_header(); ?>

<div id="content" role="main">

    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <div <?php post_class() ?> id="post-<?php the_ID(); ?>">
        <h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
        <small><?php the_time('F jS, Y') ?> <!-- by <?php the_author() ?> --></small>

        <div class="entry">
            <?php the_content(); ?>
        </div>

        <p class="postmetadata"><?php the_tags('Tags: ', ', ', '<br />'); ?> Posted in <?php the_category(', ') ?> | <?php edit_post_link('Edit', '', ' | '); ?>  <?php comments_popup_link('No Comments ?', '1 Comment ?', '% Comments ?'); ?></p>
    </div>
    <?php endwhile; endif; ?>

</div><!-- #content -->

回答by Gn??? GnurT HnA

I just simply put the_post()above the_content()and it worked

我只是简单地放在the_post()上面the_content(),它起作用了

回答by Jahmic

I'm writing this because I had a similar problem. My content wasn't showing up. However my call to the_content was inside the The Loop. Furthermore, this was working on my development server but not on the production server.

我写这个是因为我有类似的问题。我的内容没有出现。然而,我对 the_content 的调用是在 The Loop 内。此外,这适用于我的开发服务器,但不适用于生产服务器。

I was able to solve this by removing all the plugins and then, one by one, add them back in.

我能够通过删除所有插件来解决这个问题,然后一个一个地重新添加它们。

Also, of course, if you have caching enabled, a good first step is to clear the cache.

此外,当然,如果您启用了缓存,最好的第一步是清除缓存。