我一定是做错什么了。
我用一个静态的首页来设置我的网站,使用的是front-page.php。我在admin中创建了一个带有标题的页面,并在模板下拉列表中选择了front-page.php。
我的标题显示得很好,但是the_content();没有。
我没有做什么特别的事情,如下所示。
<?php
/*
Template Name: Homepage
*/ ?>
<?php get_header(); ?>
<div class="content">
<div class="welcome_area">
<div class="welcome_area_title"><?php the_title('');?></div>
<div class="welcome_area_text">
<?php the_content(); ?>
</div>知道为什么内容不会显示出来吗?
发布于 2013-04-02 01:03:26
你不是真的有圈套。
<?php get_header(); ?>
<div class="content">
<div class="welcome_area">
<div class="welcome_area_title"><?php the_title('');?></div>
<div class="welcome_area_text"><?php
if (have_posts()) {
while (have_posts()) {
the_post();
the_content();
}
} ?>正在发生的情况是:
have_posts()检查是否有post内容。如果需要,可以使用else子句提供默认内容。while(have_posts())循环遍历该内容。the_post()可以设置$post变量,也可以增加循环计数器。如果没有the_post(),就可以得到一个无限循环。这是您的代码中缺少的最关键的部分。the_post()已经运行了,post模板标记应该可以像预期的那样工作了。我没有太激进地编辑您的代码,但是我也会把这个the_title带到循环中,即使它看起来很有效。它确实是应该在环内,而且它并不总是像它之外所期望的那样工作。
发布于 2013-04-02 01:02:56
首先,如果将页面设置为静态首页,则不需要将模板与页面关联,模板也不需要页眉。WordPress自动为静态首页使用front-page.php模板,与模板层次结构相同。
但是,要回答您的问题,您需要首先调用the_post()来设置the_content()函数所依赖的全局vars。
https://wordpress.stackexchange.com/questions/94208
复制相似问题