【问题标题】:Wordpress "loop" not showing blog posts in my themeWordpress“循环”未在我的主题中显示博客文章
【发布时间】:2025-11-28 00:10:01
【问题描述】:

我制作了我的第一个主题,它非常适合编辑更新页面等,但不会显示任何帖子。

我已将“循环”放入模板页面(从二十二个主题复制),因为我只希望帖子出现在该页面上。我已将博客文章设置为显示在此页面上(来自设置页面),但仍然不会显示任何内容。

这是我用于显示博客文章的模板页面的代码。

知道有什么问题吗?

<?php
/**
*  Template Name: blog
*
* Full width page template with no sidebar.
*
* @package Myfirsttheme
* @subpackage Template
*/

get_header(); ?>

    <?php if ( have_posts() ) : ?>

        <?php /* Start the Loop */ ?>
        <?php while ( have_posts() ) : the_post(); ?>
            <?php get_template_part( 'content', get_post_format() ); ?>
        <?php endwhile; ?>

    <?php else : ?>

        <article id="post-0" class="post no-results not-found">
            <div class="entry-content">
                <p><?php _e( 'Apologies, but no results were found. Perhaps searching will help find a related post.', 'twentytwelve' ); ?></p>
                <?php get_search_form(); ?>
            </div><!-- .entry-content -->
        <?php endif; // end current_user_can() check ?>

        </article><!-- #post-0 -->

    <?php endif; // end have_posts() check ?>

    </div><!-- #content -->
</div><!-- #primary -->

<?php get_footer(); ?>

【问题讨论】:

  • 您是否将此模板分配给页面..转到页面并设置默认模板以在列出的项目中选择...
  • 两个 endif 一个 if??!!!
  • 如果这是你拥有的全部代码,你应该得到空白页,因为你有 但没有 if 语句。

标签: php html wordpress


【解决方案1】:

帖子将始终出现在 index.php 模板中,除非您更改 Settings=&gt;Reading 中的 “首页显示” 选项,例如:http://imgur.com/izwa5yw 如果您将其设置为在页面上显示博客文章(在图像中),然后任何页面(博客)都必须将默认模板(在页面编辑屏幕中)设置为您在文件的模板名称:部分中写入的值(在您的博客中),正如泰米尔语所说。

更新:您必须回显get_template_part(),否则它不会显示。您可以改用the_content(),这是首选。任何以 the_ 开头的变量都会自行输出。 get_ 变量不会自己输出。

<?php echo get_template_part(); ?>

<?php the_content() ?>

【讨论】: