2011-11-23 78 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內。此外,這在我的開發服務器上工作,但不在生產服務器上。

我能夠通過刪除所有插件,然後一個一個地解決這個問題,將它們添加回。

而且,當然,如果您啓用緩存,良好的第一步是清除緩存。