WordPress:Pages

来自站长百科
Xxf3325讨论 | 贡献2008年3月24日 (一) 09:50的版本 (新页面: ==Introduction== '''Pages''', or '''WordPress Pages''' are like Posts, except they can do much more than Posts, and they live outside of the normal blog chronology. You can use Pages to...)
(差异) ←上一版本 | 最后版本 (差异) | 下一版本→ (差异)
跳转至: 导航、​ 搜索

Introduction

Pages, or WordPress Pages are like Posts, except they can do much more than Posts, and they live outside of the normal blog chronology. You can use Pages to organize and manage any amount of content. Pages were added as a new feature in WordPress 1.5.

But how do Pages in WordPress actually operate? The purpose of this document is to attempt to explain what a Page is and what it is not, to describe what a Page can do, and to give a few examples.

Terms Used in this Article

For clarity, throughout this document:

  • "page", with a lowercase "p", will describe any HTML document on the web.
  • "Page", with a capital "P", will refer to a "WordPress Page", the feature of WordPress that this wiki page details. Use "Page", in bold, when the use of the term is ambiguous.

For further Page related nomenclature issues, see [[Wordpress:#A Note on Nomenclature|A Note on Nomenclature]].

What is a Page?

Posts are time-oriented objects. You write them at a specific time, and that time defines their context.

Pages, on the other hand, are most often used to present information about yourself or your site that is somehow timeless - information that is always applicable. For example, you might write a Post describing what you did or thought on a particular morning ("Breakfast was good"), but on a Page you might write something whose context is less time dependent ("This site is about breakfast").

Of course, this is your WordPress; you can do whatever you want with it and its features. Pages can be used to present any information you want to live "outside" your blog. Experiment and be creative. And, as always, have fun doing it!

Some examples of Pages to create on your site may include, Copyright, Legal Information, Reprint Permissions, Contact Information, About Me, About Site, Accessibility Statement, among other things.

In general, Pages are very similar to Posts in that they both have Titles and Content and can use your site's Presentation Templates to maintain a consistent look throughout your site. Pages, though, have several key distinctions which makes them quite different from Posts.

Pages in a Nutshell

What Pages Are

  • Pages are for content that is less time-dependent than Posts.
  • Pages can be organized into pages and [[Wordpress:#Organizing_your_Pages|SubPages]].
  • Pages can use different [[Wordpress:#Page Templates|Page Templates]] which can include Template Files, Wordpress:Template Tags and other PHP code.

What Pages are Not

  • Pages are not Posts, nor are they excerpted from larger works of fiction. They do not cycle through your blog's main page. (Note: You can include Posts in Pages by using the Inline Posts Plugin.)
  • Pages cannot be associated with Categories and cannot be assigned Tags. The organizational structure for Pages comes only from their hierarchical interrelationships, and not from Tags or Categories.
  • Pages are not files. They are stored in your database just like Posts are.
  • Although you can put Template Tags and PHP code into a Page Template, you cannot put these into the content of a Page and expect them to run. (Note: You can achieve this by using a PHP evaluating Plugin such as Exec-PHP or RunPHP.)

Creating Pages

To create a new Page, log in to your WordPress installation with sufficient admin privileges to create new articles. In the Administration Panel, choose the Write Panel and then choose the Write Page SubPanel to access the page where you can create your new Page.

Listing Your Pages on Your Site

WordPress is able to automatically generate a list of Pages on your site, for example within the sidebar, using a Template Tag called wp_list_pages(). See the wp_list_pages page for information on how to

  • sort the list of Pages (to fully customize the order in which the Pages are listed, you might find the "Page Order" field on the Write > Write Page administration panel useful),
  • exclude (or 'hide') a Page from the list,
  • control which Pages are displayed (i.e., all Pages or just certain SubPages), and
  • control how deep into your Page hierarchy the list goes.

Naturally, you can also link to Pages manually with an HTML link. For example, if you want your Copyright Page listed in your footer, that link might read as below:
If you do not have Permalinks set up

<a title="Copyright information" href="wordpress/?page_id=14">Copyright 1996-2006</a>

If you do have Permalinks set up

<a title="Copyright information" href="wordpress/copyright/">Copyright 1996-2006</a>

Note: Your .htaccess file must be writeable for Page Permalinks to work, otherwise you must update your .htaccess file every time you create a Page.

Organizing Your Pages

Just as you can have Subcategories within your Categories, you can also have SubPages within your Pages, creating a hierarchy of pages.

For example, suppose you are creating a WordPress site for a travel agent and would like to create an individual Page for each continent and country to which the agency can make travel arrangements. You would begin by creating a Page called "Africa" on which you could describe general information about travel to Africa. Then you would create a series of Pages which would be SubPages to "Africa" and might include "Lesotho", "Cameroon", "Togo", and "Swaziland". Another individual Page is made for "South America" and would feature SubPages of "Brazil", "Argentina", and "Chile". Your site would then list:

  • Africa
    • Cameroon
    • Lesotho
    • Swaziland
    • Togo
  • South America
    • Argentina
    • Brazil
    • Chile

To begin the process, go to Administration > Write > Write Page panel, in the upper right corner of the panel and click the "Page Parent" drop-down menu. The drop-down menu contains a list of all the Pages already created for your site. To turn your current Page into a SubPage, or "Child" of the "Parent" Page, select the appropriate Page from the drop-down menu. If you specify a Parent other than "Main Page (no parent)" from the list, the Page you are now editing will be made a Child of that selected Page. When your Pages are [[Wordpress:#Listing Your Pages on Your Site|listed]], the Child Page will be nested under the Parent Page. The Permalinks of your Pages will also reflect this Page hierarchy.

In the above example, the Permalink for the Cameroon Page would be:

http://example.com/africa/cameroon/

Page Templates

Individual Pages can be set to use a specific custom Page Template (a PHP template file, e.g., snarfer.php) you create within your Theme (see [[Wordpress:#Creating your own Page Templates|Creating your own Page Templates]] below on how to create a custom template). This new Page Template will then override the default page.php Page Template included with your Theme. See [[Wordpress:#What Template is used to Display a Particular Page?|What Template is used to Display a Particular Page?]] below, to find out exactly which Template will be used, but read the following first, so you understand the answer :)

WordPress can be configured to use different Page Templates for different Pages. Toward the bottom of the Write > Write Page administration panel (or on the sidebar, depending on which version of WordPress you are using) is a drop-down labeled "Page Template". From there you can select which Template will be used when displaying this particular Page.

NOTE: In order to access the Page Template selector, there must be at least one custom Page Template available in the active theme (see [[Wordpress:#Creating your own Page Templates|Creating your own Page Templates]] below to learn how to create one).

Default Theme Page Templates

The Default theme contains three Page Templates for your use:

  • page.php - Default Page Template: displays Page content
  • archives.php - ignores Page content and instead displays a list of Archives by Month and Archives by Subject (by Category)
  • links.php - ignores Page content and instead displays your links using get_links_list

What Template is used to Display a Particular Page?

WordPress will look for several template files in your active Theme. The first one it finds will be used to display any given Page. WordPress will look for files in the following order:

  1. The Page's selected "Page Template"
  2. page.php
  3. index.php

Creating Your Own Page Templates

The files defining each Page Template are found in your Themes directory. To create a new Template for a Page you must create a file. Let's call our first Page Template for our Page snarfer.php. At the top of the snarfer.php file, put the following:

<?php
/*
Template Name: Snarfer
*/
?>

The above code defines this snarfer.php file as the "Snarfer" Template. Naturally, "Snarfer" may be replaced with most any text to change the name of the Page Template. This Template Name will appear in the Theme Editor as the link to edit this file.

The file may be named almost anything with a .php extension (see reserved Theme filenames for filenames you should not use; these are special file names WordPress reserves for specific purposes).

What follows the above five lines of code is up to you. The rest of the code you write will control how Pages that use the Snarfer Page Template will display. See Wordpress:Template Tags for a description of the various WordPress Template functions you can use for this purpose. You may find it more convenient to copy some other Template (perhaps page.php or index.php) to snarfer.php and then add the above five lines of code to the beginning of the file. That way, you will only have to alter the HTML and PHP code, instead of creating it all from scratch. Examples are shown [[Wordpress:#Examples|below]]. Once you have created the Page Template and placed it in your Theme's directory, it will be available as a choice when you create or edit a Page. (Note: when creating or editing a Page, the Page Template option does not appear unless there is at least one template defined in the above manner.)

==Examples of Pages and Templates==

The following is a list of instructional examples. Feel free to make additions.

Archives with Content

A Page Template that shows the Page's content at the top, and then displays a list of archive months and categories below it. This is designed to work with WordPress's Default theme (aka Kubrick), but will probably work with many other themes with a little modification.

Save this to arc-cont.php:

<?php
/*
Template Name: Archives with Content
*/
?>

<?php get_header(); ?>

<div id="content" class="widecolumn">

 <?php if (have_posts()) : while (have_posts()) : the_post();?>
 <div class="post">
  <h2 id="post-<?php the_ID(); ?>"><?php the_title();?></h2>
  <div class="entrytext">
   <?php the_content('<p class="serif">Read the rest of this page &raquo;</p>'); ?>
  </div>
 </div>
 <?php endwhile; endif; ?>
 <?php edit_post_link('Edit this entry.', '<p>', '</p>'); ?>

</div>
<div id="main">

<?php include (TEMPLATEPATH . '/searchform.php'); ?>

<h2>Archives by Month:</h2>
  <ul>
    <?php wp_get_archives('type=monthly'); ?>
  </ul>

<h2>Archives by Subject:</h2>
  <ul>
     <?php wp_list_cats(); ?>
  </ul>

</div>
<?php get_footer(); ?>

WordPress as a CMS

With the new Pages feature in 1.5+, it's easy to use WordPress for basic content management.

Using a Page as the Front Page (version 2.1+ only)

WordPress 2.1 introduced the Option to conveniently set any Page as your Front Page. Go to Options > Reading in the Wordpress Admin interface. Under the Front Page Category, you can choose to set any (published) Page or Posts Page as the Front Page. The default setting shows your blog with the latest blog posts.

Note: In 2.1, the conditional is_home() function no longer returns true for the Front Page set in Admin. In order to emulate the pre-2.1 function of returning true for the "front" home page, use the is_frontpage plugin or use is_page(home) where the slug of your desired front page is "home".

The following paragraphs explain how to duplicate this functionality for versions prior to v2.1.

Using a Page as the Front Page with WordPress Versions Prior to v2.1

Using the Static Front Page Plugin, it is possible to set any Page as the "front page" of your site. The Plugin modifies the home page query and sticks the Page with a Page slug of "home" to the front page.

When the Page is being displayed as the homepage, if a Page Template with the filename home.php exists for your active Theme, the Plugin will override the Page's set Page Template and use home.php instead. The Page's set Page Template will still apply if the Page is visited like a standard Page (e.g., http://example.com/home/)

As an alternative to the Plugin, WordPress will recognize a "home.php" document in your template directory and use it rather than index.php to theme your front page. However, if your home page isn't your blog, be advised that the home.php template will always apply to the blog page whether you like it or not. Using the query_posts() template tag, you can call on any page before you invoke Wordpress:The Loop. For instance:

<?php 
     query_posts('pagename=home'); 
?>

Will call up only the post with the pagename "home." See query_posts() for more examples of the template tag in action.

The home.php file is not needed in versions 2.1+ . Use the setting in the control panel to set your static home page.

Including a Page

You might also want to include Pages in various places on your site. That way, you can have an easy way to edit elements of your website. There is a Plugin called Include Page that makes doing this easy. Improved Include Page is a more updated version of the Plugin.

Making Your Blog Appear in a Non-Root Folder

Suppose you have WordPress running at http://example.com/. Also, suppose that you want your blog to be located at http://example.com/blog/ and that you want other pages to be available at http://example.com/page1/.

The first thing you will want to do is to create a homepage. You can do this by creating home.php in your theme directory. Next, create a blog template. The easiest way to do this is to create a file named blog.php with the following contents in your theme directory:

<?php
/*
Template Name: Blog
*/

// Which page of the blog are we on?
$paged = get_query_var('paged');
query_posts('cat=-0&paged='.$paged);

// make posts print only the first part with a link to rest of the post.
global $more;
$more = 0;

//load index to show blog
load_template(TEMPLATEPATH . '/index.php');
?>

Log in to WordPress and create a page named "Blog" with Template "blog". You're done. The one last thing you will want to do is to update your permalinks structure to begin with "/blog/", i.e., "/blog/%year%/%monthnum%/%postname%/". Now you have a WordPress-managed CMS with a unique front page and logically-structured blog content.

If you're using the <!-- more --> quicktag to place things below the fold in your posts, you'll want to include the global variable $more and set it to zero (as shown in the code above). Otherwise, the entire post will print out on your blog's main page.

As noted above, the home.php file is not needed in versions 2.1+. If both the blog.php and home.php files are deployed in 2.1+ they will conflict and cause the blog.php templated page to appear blank.

A Note on Nomenclature

A page can be static or dynamic. Static pages are those which have been created once and do not have to be regenerated every time a person visits it. In contrast, dynamic pages do need to be regenerated every time they are viewed; code for what to generate has been specified by the author, but not the actual page itself. These use extensive PHP code which is evaluated each time the page is visited, and the content is thus generated on the fly, upon each new visit.

Almost everything in WordPress is generated dynamically, including Pages. Everything you and others write in WordPress (Posts, Pages, Comments, Blogrolls, Categories, etc.) is stored in your MySQL database. When your site is accessed, that database information is then used by your WordPress Templates from your current Theme to generate the web page being requested. Thus, WordPress information is dynamic, including the information contained in your Pages.

An example of a static page might be an HTML document (without any PHP code) you've written as an addition to your dynamically generated WordPress pages, perhaps an "About Me" page. The problem with purely static pages is that they are difficult to maintain. Changes you make to your WordPress settings, Themes and Templates will not be propagated to pages coded only in HTML. The Page feature of WordPress was developed, in part, to alleviate this problem. By using Pages, users no longer have to update their static pages every time they change the style of their site. Instead, if written properly, their dynamic Pages will update themselves along with the rest of your blog.

Despite the dynamic nature of Pages, many people refer to them as being static. In the context of web publishing, static and dynamic mean what has been described above. More generally, however, static can mean "characterized by a lack of change". It is easy to see how this definition influenced the word's use in describing types of web pages. It is also to easy to see why people think of Pages as being static; Posts come and go, but Pages are here to stay since Pages are typically used to display information about your site which is constant (e.g., information about yourself, description of your site).

In other words, a Page contains static information but is generated dynamically. Thus, either "static" or "dynamic" may be validly used to describe the nature of the WordPress Page feature. However, in order to avoid confusion, and because Pages themselves are dynamic while it is only their contents which are in some way static, this document does not refer to Pages as being static.