WordPress.org

Ready to get started?Download WordPress

Codex

Attention Interested in functions, hooks, classes, or methods? Check out the new WordPress Code Reference!

Difference between revisions of "Pages"

(fixing some major wrong things added to article. Also, WordPress has cap!)
(fix links)
Line 5: Line 5:
 
{{sk|Stránky}}
 
{{sk|Stránky}}
 
{{th|Pages}}
 
{{th|Pages}}
{{zh-cn|??}}
+
{{zh-cn|页面}}
{{zh-tw|????}}
+
{{zh-tw|網誌分頁}}
{{ru|????????}}
+
{{ru|Страницы}}
 
{{pt-br|Sub-Painel Páginas-Adicionar Nova}}
 
{{pt-br|Sub-Painel Páginas-Adicionar Nova}}
 
}}
 
}}

Revision as of 13:28, 19 June 2012

In WordPress, you can write either posts or pages. When you're writing a regular blog entry, you write a post. Posts automatically appear in reverse chronological order on your blog's home page. Pages, on the other hand, are for content such as "About Me," "Contact Me," etc. Pages live outside of the normal blog chronology, and are often used to present information about yourself or your site that is somehow timeless -- information that is always applicable. You can use Pages to organize and manage any amount of content.

Other examples of common pages include Copyright, Legal Information, Reprint Permissions, Company Information, and Accessibility Statement. (By the way, it's a good idea to always have an about page and a contact page -- see this advice from Lorelle.)

In general, Pages are very similar to Posts in that they both have Titles and Content and can use your WordPress Theme templates files to maintain a consistent look throughout your site. Pages, though, have several key distinctions that make 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 SubPages.
  • Pages can use different Page Templates which can include Template Files, Template Tags and other PHP code.
  • Pages may have a more complex array of readily available display adjustments when using certain sophisticated Themes, such as Weaver 2.2.
  • In essence, Pages are for non-blog content. It is possible to remove all or most Posts from a WordPress installation, and thus to create a standard non-blog website.

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. WordPress Plugins are available to change the default if necessary.
  • 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 file, you cannot put these into the Page or Post content without a WordPress Plugin like Exec-PHP which overwrites the code filtering process.
  • Pages are not included in your site's feed.
  • Pages and Posts may attract attention in different ways from humans or search engines.
  • Pages (or a specific post) can be set as a [[Creating_a_Static_Front_Page|static front page] if desired with a separate Page set for the latest blog posts, typically named "blog."

Creating Pages

To create a new Page, log in to your WordPress installation with sufficient admin privileges to create new articles. Select the Administration > Pages > Add New option to begin writing a new Page.

Pages > Add New

Changing the URL of Your Pages

To change the URL or "slug" of a Page, use the Edit Slug feature under the Page title on the WordPress Administration > Page edit screen.

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 do the following:

  • Sort the list of Pages (to fully customize the order in which the Pages are listed, you might find the "Page Order" section on the Pages > Add New administration panel
  • 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-2010</a>

If you do have Permalinks set up

<a title="Copyright information" href="wordpress/copyright/">Copyright 1996-2010</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 > Pages > Add New 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 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 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 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 > 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 Creating your own Page Templates below to learn how to create one). If a custom page exists, but you still are not able to see Page Template selector, try to re-activate your current theme.

Default Theme Page Templates

The Default (TwentyEleven) 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 wp_list_bookmarks()

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 Custom Page 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 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 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_categories(); ?>
 </ul>

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

A Page of Posts

The following template simply displays posts from two specific categories (specified by their category slugs). It is designed to work within a child of the default Twenty Eleven theme. If you are using another theme, you need to replicate the html structure of your own theme within the template.

Save this to pageofposts.php and then assign the Page of Posts Template to your new Page:

<?php
/*
Template Name: Page Of Posts
*/

// if you are not using this in a child of Twenty Eleven, you need to replicate the html structure of your own theme.

get_header(); ?>
<div id="primary">
<div id="content" role="main">

<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args= array(
	'category_name' => 'antiquarianism, championship', // Change these category SLUGS to suit your use.
	'paged' => $paged
);
query_posts($args);
if( have_posts() ) :?>

<?php twentyeleven_content_nav( 'nav-above' );?>

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

<?php get_template_part( 'content', get_post_format() ); ?>

<?php endwhile; ?>

<?php twentyeleven_content_nav( 'nav-below' ); ?>

<?php else : ?>
<article id="post-0" class="post no-results not-found">
<header class="entry-header">
<h1 class="entry-title"><?php _e( 'Nothing Found', 'twentyeleven' ); ?></h1>
</header><!-- .entry-header -->

<div class="entry-content">
<p><?php _e( 'Apologies, but no results were found for the requested archive. Perhaps searching will help find a related post.', 'twentyeleven' ); ?></p>
<?php get_search_form(); ?>
</div>
</article>

<?php endif; ?>

</div>
</div>

<?php get_footer();

Using Custom Fields

A Page Template that displays posts from a specific category depending on a Custom Field assigned to a Page. In this example the value of the Custom Field "category" is retrieved and used as the category to retrieve the posts in that category. So if the category of posts you want to display is called "Events" then assign the Custom Field "category" with a value of "Events" to the Page. Note that this will adhere to pagination rules meaning that four (4) posts will display per page with links to older/newer posts provided. This is designed to work with the WordPress Default theme (aka Kubrick), but should work with other themes with a little modification.

Save this to pageofposts.php and then assign PageofPosts as the Template when creating the action Page:

<?php
/*
Template Name: PageOfPosts
*/

get_header(); ?>

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

<?php
if (is_page() ) {
$category = get_post_meta($posts[0]->ID, 'category', true);
}
if ($category) {
 $cat = get_cat_ID($category);
 $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
 $post_per_page = 4; // -1 shows all posts
 $do_not_show_stickies = 1; // 0 to show stickies
 $args=array(
  'category__in' => array($cat),
  'orderby' => 'date',
  'order' => 'DESC',
  'paged' => $paged,
  'posts_per_page' => $post_per_page,
  'ignore_sticky_posts' => $do_not_show_stickies
 );
 $temp = $wp_query; // assign orginal query to temp variable for later use  
 $wp_query = null;
 $wp_query = new WP_Query($args); 
 if( have_posts() ) : 
		while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
	  <div <?php post_class() ?> id="post-<?php the_ID(); ?>">
    <h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
    <small><?php the_time('F jS, Y') ?> <!-- by <?php the_author() ?> --></small>
    <div class="entry">
     <?php the_content('Read the rest of this entry »'); ?>
    </div>
    <p class="postmetadata"><?php the_tags('Tags: ', ', ', '<br />'); ?> Posted in <?php the_category(', ') ?> | <?php edit_post_link('Edit', '', ' | '); ?> <?php comments_popup_link('No Comments »', '1 Comment »', '% Comments »'); ?></p>
   </div>
  <?php endwhile; ?>
  <div class="navigation">
   <div class="alignleft"><?php next_posts_link('« Older Entries') ?></div>
   <div class="alignright"><?php previous_posts_link('Newer Entries »') ?></div>
  </div>
 <?php else : ?>

		<h2 class="center">Not Found</h2>
		<p class="center">Sorry, but you are looking for something that isn't here.</p>
		<?php get_search_form(); ?>

	<?php endif; 
	
	$wp_query = $temp; //reset back to original query
	
} // if ($category)
?>

	</div>

<?php get_sidebar(); ?>

<?php get_footer(); ?>


Using Custom Post Types

A Page displaying the posts belonging to a custom post type. In this case, the custom post type is book. The Template below is designed to work with the WordPress Twenty Fourteen theme but is best used as a Twenty Ten Child Theme.

<?php
/**
 * Template Name: Page of Books
 *
 * Selectable from a dropdown menu on the edit page screen.
 */
?>

<?php get_header(); ?>

		<div id="container">
			<div id="content">
<?php 
$type = 'book';
$args=array(
 'post_type' => $type,
 'post_status' => 'publish',
 'paged' => $paged,
 'posts_per_page' => 2,
 'ignore_sticky_posts'=> 1
);
$temp = $wp_query; // assign orginal query to temp variable for later use  
$wp_query = null;
$wp_query = new WP_Query($args); 
?>

<?php

 get_template_part( 'loop', 'index' );?>
			</div><!-- #content -->
		</div><!-- #container -->

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

WordPress as a CMS

You can use WordPress for basic content management. If you do, you'll probably create a large number of pages for your content.

Using a Page as the Front Page

A Page can easily be set to be your site's Front Page. Visit the Administration > Settings > Reading panel and under Front page displays, 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.

Templates by page-ID or page-Slug

You can use different archives "page", with ID or slug in your name. Only Available starting with Version 2.9.

examples:

  • page-{id}.php
  • page-{slug}.php

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 Improved Include Page that makes doing this easy.

The Dynamic Nature of WordPress "Pages"

A web page can be static or dynamic. Static pages, such as a regular HTML page that you might create with Dreamweaver, are those which have been created once and do not have to be regenerated every time a person visits it. In contrast, dynamic pages, such as those you create with WordPress, 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 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.