2011-11-23 79 views
14

我创建一个自定义的WordPress主题,我似乎无法得到single.php模板工作。以下是我写的代码。标题出现了,但内容却没有。任何想法,为什么不是?WordPresspress:single.php不显示the_content()

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

在这里看到输出的截图:

enter image description here

回答

40

the_content()无法显示,因为它是内的循环 - take a look at the docs here »

您需要将您的代码更改为:

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

你可以离开了else如果你总是相信你有内容显示:)或只采取看原来single.php在这里你可以找到的循环始终围绕着the_content()

编辑:

这里是整个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 --> 
+0

我不能谢谢你!!!谢谢 –

+0

感谢您的回答 – LazyCatIT

+0

这让我疯狂!从单个页面显示内容时,我不得不使用循环。 – Maximus

1

我正在写这个,因为我有类似的问题。我的内容没有显示出来。但是我的调用the_content在The Loop内。此外,这在我的开发服务器上工作,但不在生产服务器上。

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

而且,当然,如果您启用缓存,良好的第一步是清除缓存。