为什么这个 Wordpress page.php 文件包含帖子循环? [英] Why this Wordpress page.php file contain the posts loop?

查看:19
本文介绍了为什么这个 Wordpress page.php 文件包含帖子循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

<div id="primary" class="content-area"><div id="content" class="site-content" role="main"><?php/* 循环 */?><?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();?>

<?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:','二十十三') . '</span>', 'after' => '</div>', 'link_before' => '<span>', 'link_after' => '</跨度>'));?></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();?>

为什么?

解决方案

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

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

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

因此,您通常会在 WordPress 中看到用于任何给定模板类型的帖子"循环",即使您实际上不会多次循环,即使您实际上不是输出博文.

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

如果您正在开发一个主题,您需要非常熟悉模板层次结构loop 一起输出 WordPress 内容.

I am pretty new in WordPress theme development and I have the following question about the page.php page contained into templates files (the file that describe the template for the static pages)

I am analazyng the page.php file contained into the twentythirten default theme, the code of this file is:

<?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(); ?>

My doubts are:

1) page.php is the default template for the static pages but this file contain the posts loop:

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

Why?

解决方案

WordPress is designed so that a very basic theme only needs one template: index.php. If you create a theme with an index.php, that file will be used for rendering all your content: the archives, the main blog page, individual pages, and so on. And all it needs on it to basically work is the loop.

To achieve this, virtually every piece of content in WordPress is technically a "post". Blog posts are posts. Individual pages are "posts". Even search results are "posts". And all you need to do to display them is loop through the objects that WordPress hands you and output them.

Obviously, it's more helpful if different types of posts are displayed in different ways, so WordPress provides ways of overriding this default behaviour. If you add a page.php template, then WordPress will invoke that for individual pages instead of the default index.php. See the Template Hierarchy diagram to see which template page will be used for a given type of post. (But note that they all fall through to index.php if no specific template exists.)

Because of this, you'll normally see a "post" "loop" used for any given template type in WordPress, even if you're not actually going to loop more than once, and even if you're not actually outputting blog posts.

On a single Page, WordPress will only "loop" once at most, and you can probably assume that you'll always have one post to output, so if you really want to, you can replace the usual while loop with code to output a single post. However, because WordPress still hands you a "set" of posts that just happens to contain a single post, you still need to at least call the_post() to load up this single post into the variables that are used for the later output calls like the_content(). As the code is therefore virtually identical with a loop, and most people have already written the loop for their index.php template, normally people just leave it as a loop, knowing it'll only execute once.

If you're developing a theme, you'll want to get very familiar with how the template hierarchy and loop work together to output WordPress content.

这篇关于为什么这个 Wordpress page.php 文件包含帖子循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆