【问题标题】:Wordpress: single.php doesn't display the_content()Wordpress:single.php 不显示 the_content()
【发布时间】:2012-01-04 23:49:36
【问题描述】:

我正在创建一个自定义 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 -->

查看这里的输出截图:

【问题讨论】:

    标签: php wordpress wordpress-theming


    【解决方案1】:

    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 Loop 始终围绕着 @987654326 @

    编辑:

    这是您可能想要使用/开始的整个 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 -->
    

    【讨论】:

    • 这快把我逼疯了!对我来说,在显示单个页面的内容时必须使用循环是没有意义的。
    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

      我写这篇文章是因为我遇到了类似的问题。我的内容没有显示。然而我的调用 the_content 是在循环内。此外,这在我的开发服务器上有效,但在生产服务器上无效。

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

      当然,如果您启用了缓存,第一步是清除缓存。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-20
        • 1970-01-01
        相关资源
        最近更新 更多