简体   繁体   中英

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

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?

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

在此处输入图片说明

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

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 else if you are always sure you have content to display :) Or just take look at the original single.php where you can find The Loop always surrounds the_content()

edit:

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

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

我只是简单地把the_post()以上the_content()和它的工作

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM