【问题标题】:Why this Wordpress page.php file contain the posts loop?为什么这个 Wordpress page.php 文件包含帖子循环?
【发布时间】:2014-07-14 08:48:22
【问题描述】:

我是 WordPress 主题开发的新手,我有以下关于模板文件中包含的 page.php 页面(描述静态页面模板的文件)的问题

我正在分析 twentythirten 默认主题中包含的 page.php 文件,该文件的代码是:

<?php
/**
 * The template for displaying all pages
 *
 * This is the template that displays all pages by default.
 * Please note that this is the WordPress construct of pages and that other
 * 'pages' on your WordPress site will use a different template.
 *
 * @package WordPress
 * @subpackage Twenty_Thirteen
 * @since Twenty Thirteen 1.0
 */

get_header(); ?>

    <div id="primary" class="content-area">
        <div id="content" class="site-content" role="main">

            <?php /* The loop */ ?>
            <?php while ( have_posts() ) : the_post(); ?>

                <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
                    <header class="entry-header">
                        <?php if ( has_post_thumbnail() && ! post_password_required() ) : ?>
                        <div class="entry-thumbnail">
                            <?php the_post_thumbnail(); ?>
                        </div>
                        <?php endif; ?>

                        <h1 class="entry-title"><?php the_title(); ?></h1>
                    </header><!-- .entry-header -->

                    <div class="entry-content">
                        <?php the_content(); ?>
                        <?php wp_link_pages( array( 'before' => '<div class="page-links"><span class="page-links-title">' . __( 'Pages:', 'twentythirteen' ) . '</span>', 'after' => '</div>', 'link_before' => '<span>', 'link_after' => '</span>' ) ); ?>
                    </div><!-- .entry-content -->

                    <footer class="entry-meta">
                        <?php edit_post_link( __( 'Edit', 'twentythirteen' ), '<span class="edit-link">', '</span>' ); ?>
                    </footer><!-- .entry-meta -->
                </article><!-- #post -->

                <?php comments_template(); ?>
            <?php endwhile; ?>

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

<?php get_sidebar(); ?>
<?php get_footer(); ?>

我的疑惑是:

1) page.php 是静态页面的默认模板,但此文件包含帖子循环:

 <?php while ( have_posts() ) : the_post(); ?>

为什么?

【问题讨论】:

  • Pages 也称为 Post,但其 Post 类型是 Pages
  • 虽然 page.php 并不真正保证“循环”,因为它通常只处理单个页面,但在 WP 开发中使用循环似乎是传统的。在“page.php”上,这个循环通常最多执行一次。我猜想使用循环是传统的做法,因为您通常首先编写一个模板—index.php,它有一个循环,然后将其复制为任何更具体模板的起点(如果您不提供page.php,则默认会使用 index.php,因此将 index.php 复制粘贴到 page.php 是开始开发自定义 page.php 的正常方式。)

标签: php wordpress-theming wordpress


【解决方案1】:

WordPress 的设计使得一个非常基本的主题只需要一个模板:index.php。如果您使用index.php 创建主题,该文件将用于呈现您的所有内容:档案、主博客页面、单个页面等。基本上它所需要的只是循环。

为了实现这一点,WordPress 中的几乎所有内容在技术上都是一个“帖子”。博客文章是文章。单个页面是“帖子”。甚至搜索结果都是“帖子”。显示它们所需要做的就是遍历 WordPress 提供给您的对象并输出它们。

显然,如果不同类型的帖子以不同的方式显示会更有帮助,因此 WordPress 提供了覆盖此默认行为的方法。如果您添加 page.php 模板,那么 WordPress 将为单个页面调用该模板,而不是默认的 index.php。请参阅Template Hierarchy diagram 以了解哪个模板页面将用于给定类型的帖子。 (但请注意,如果不存在特定模板,它们都属于index.php。)

因此,您通常会在 WordPress 中看到用于任何给定模板类型的“post”“循环”,即使您实际上不会多次循环,即使您实际上并没有输出博客文章。

在单个页面上,WordPress 最多只会“循环”一次,并且您可能会假设您总是有一个帖子要输出,所以如果您真的想要,您可以替换通常的 while 循环使用代码输出单个帖子。但是,由于 WordPress 仍然为您提供恰好包含单个帖子的“一组”帖子,您仍然需要至少调用 the_post() 将这个帖子加载到用于稍后输出调用的变量中,例如the_content()。由于代码实际上与循环相同,并且大多数人已经为他们的 index.php 模板编写了循环,通常人们只是将其保留为循环,知道它只会执行一次。

如果您正在开发主题,您需要非常熟悉 template hierarchyloop 如何协同工作以输出 WordPress 内容。

【讨论】:

  • 完美的解释。最后一个问题,看看我是否真的明白你对我说的话。如果我从 page.php 文件中删除我的 while 循环,我仍然会看到静态页面的内容。发生这种情况是因为如果我删除循环使用继承的 index.php 页面的循环? Tnx
【解决方案2】:

WordPress 有多种类型的“帖子”。默认的“帖子类型”是 pageattachmentnav_menu_itempostrevision.

您甚至可以使用register_post_type() 函数创建自己的帖子类型。以这种方式注册的帖子类型称为“自定义帖子类型”。

参考:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-11
    • 2021-04-25
    • 1970-01-01
    • 2014-07-28
    相关资源
    最近更新 更多