document3

9
Theme Handbook Theme Handbook Page Templates Page templates are a specic type of template le that can be applied to a specic page or groups of pages. Since a page template is a specic type of template le, here are some distinguishing features of page templates: Page templates only apply to pages, not to any other content type (like posts and custom post types). Page templates are used to change the look and feel of a page. A page template can be applied to a single page, a page section, or a class of pages. CHAPTERS 1 Getting Started Who Should Read This Handbook? What is a Theme? WordPress Licensing & the GPL Setting up a Development Environment Theme Development Examples 2 Theme Basics Template Files Post Types Organizing Theme Files Template Hierarchy Template Tags The Loop Theme Functions Linking Theme Files & Browse: Home / Theme Handbook / Template Files Section / Page Template Files / Page Templates TOPICS Uses for Page Templates Page Templates within the Template Hierarchy Page Templates Purpose & User Control File Organization of Page Templates Creating Custom Page Templates for Global Use Creating a Custom Page Template for One Specic Page Using Conditional Tags in Page Templates Identifying a Page Template Page Template Functions WordPress › Page Templates | Theme Developer Handbook | ... https://developer.wordpress.org/themes/template-files-section... 1 von 9 13.06.15 12:36

Upload: capone83

Post on 16-Sep-2015

216 views

Category:

Documents


0 download

DESCRIPTION

doc

TRANSCRIPT

  • Theme HandbookTheme Handbook

    Page TemplatesPage templates are aspecic type of templatele that can be appliedto a specic page orgroups of pages.

    Since a page template isa specic type oftemplate le, here aresome distinguishingfeatures of pagetemplates:

    Page templatesonly apply topages, not to anyother content type(like posts andcustom posttypes).Page templatesare used tochange the lookand feel of a page.A page templatecan be applied toa single page, apage section, or aclass of pages.

    C H A P T E R S

    1

    Getting StartedWho Should Read

    This Handbook?

    What is a Theme?

    WordPress

    Licensing & the

    GPL

    Setting up a

    Development

    Environment

    Theme

    Development

    Examples

    2

    Theme BasicsTemplate Files

    Post Types

    Organizing Theme

    Files

    Template

    Hierarchy

    Template Tags

    The Loop

    Theme Functions

    Linking Theme

    Files &

    Browse: Home / Theme Handbook / Template Files Section / Page Template Files / Page Templates

    T O P I C S

    Uses for PageTemplates

    Page Templateswithin the TemplateHierarchy

    Page TemplatesPurpose & UserControl

    File Organization ofPage Templates

    Creating CustomPage Templates forGlobal Use

    Creating a CustomPage Template forOne Specic Page

    Using ConditionalTags in PageTemplates

    Identifying a PageTemplate

    Page TemplateFunctions

    WordPress Page Templates | Theme Developer Handbook | ... https://developer.wordpress.org/themes/template-files-section...

    1 von 9 13.06.15 12:36

  • Page templates generally have a high level ofspecicity, targeting an individual page or groupof pages. For example, a page templatenamed page-about.php is more specic thanthe template les page.php or index.php as itwill only aect a page with the slug of about.If a page template has a template name,WordPress users editing the page have controlover what template will be used to render thepage.

    Uses for Page Templates #Page templates display your sites dynamic content ona page, e.g., posts, news updates, calendar events,media les, etc. You may decide that you want yourhomepage to look a specic way, that is quite dierentto other parts of your site. Or, you may want to displaya featured image that links to a post on one part of thepage, have a list of latest posts elsewhere, and use acustom navigation. You can use page templates toachieve these things.

    This section shows you how to build page templatesthat can be selected by your users through their adminscreens.

    For example, you can build page templates for:

    full-width, one-columntwo-column with a sidebar on the righttwo-column with a sidebar on the leftthree-column

    Top

    Page Templates within the TemplateHierarchy #When a person browses to your website, WordPressselects which template to use for rendering that page.As we learned earlier in the Template Hierarchy,WordPress looks for template les in the following

    Directories

    Including CSS &

    JavaScript

    Conditional Tags

    3

    Template Files SectionPost Template

    Files

    Page Template

    Files

    Page

    Templates

    Attachment

    Template Files

    Custom Post Type

    Template Files

    Partial and

    Miscellaneous

    Template Files

    4

    Theme FunctionalityCore-Supported

    Features

    Custom

    Headers

    Post

    Formats

    Sidebars

    Widgets

    Navigation Menus

    Categories, Tags, &

    Custom

    Taxonomies

    Taxonomy

    Templates

    Pagination

    Comments

    WordPress Page Templates | Theme Developer Handbook | ... https://developer.wordpress.org/themes/template-files-section...

    2 von 9 13.06.15 12:36

  • order:

    Page Template If the page has a customtemplate assigned, WordPress looks for that leand, if found, uses it.

    1.

    page-{slug}.php If no custom template hasbeen assigned, WordPress looks for and uses aspecialized template that contains the pagesslug.

    2.

    page-{id}.php If a specialized template thatincludes the pages slug is not found, WordPresslooks for and uses a specialized template namedwith the pages ID.

    3.

    page.php If a specialized template thatincludes the pages ID is not found, WordPresslooks for and uses the themes default pagetemplate.

    4.

    index.php If no specic page templates areassigned or found, WordPress defaults back tousing the themes index le to render pages.

    5.

    Top

    Page Templates Purpose & User Control#If you plan on making a custom page template for yourtheme, you should decide a couple of things beforeproceeding:

    Whether the page template will be for onespecic page or for any page; andWhat type of user control you want available forthe template.

    Every page template that has a template name can be

    Media

    Featured Images &

    Post

    Thumbnails

    Internationalization

    Localization

    Accessibility

    5

    Advanced Theme TopicsChild Themes

    Theme Options

    The Customizer

    API

    Theme Security

    UI Best Practices

    JavaScript Best

    Practices

    Theme Testing

    Validating Your

    Theme

    Plugin API Hooks

    6

    Releasing Your ThemeRequired Theme

    Files

    Testing

    Theme Review

    Guidelines

    Writing

    Documentation

    Submitting Your

    Theme to

    WordPress.org

    7

    Credits

    Alert: There is also a WordPress-denedtemplate named paged.php. It is not usedfor the page post-type but rather fordisplaying multiple pages of archives.

    WordPress Page Templates | Theme Developer Handbook | ... https://developer.wordpress.org/themes/template-files-section...

    3 von 9 13.06.15 12:36

  • selected by a user when they create or edit a page. Thelist of available templates can be found at Pages > AddNew > Attributes > Template. Therefore, a WordPressuser can choose any page template with a templatename, which might not be your intention.

    For example, if you want to have a specic templatefor your About page, it might not be appropriate toname that page template About Template as it wouldbe globally available to all pages (i.e. the user couldapply it to any page). Instead, create a single usetemplate and WordPress will render the page with theappropriate template, whenever a user visits theAbout page.

    Conversely, many themes include the ability to choosehow many columns a page will have. Each of theseoptions is a page template that is available globally. Togive your WordPress users this global option, you willneed to create page templates for each option andgive each a template name.

    Dictating whether a template is for global use vs.singular use is achieved by the way the le is namedand whether or not is has a specic comment.

    Top

    File Organization of Page Templates #As discussed in Theme Files Organization WordPress

    Note: Sometimes it is appropriate to havea template globally available even if itappears to be a single use case. Whenyoure creating themes for release, it canbe hard to predict what a user willname their pages. Portfolio pages are agreat example as not every WordPressuser will name their portfolio the samething or have the same page ID and yetthey may want to use that template.

    WordPress Page Templates | Theme Developer Handbook | ... https://developer.wordpress.org/themes/template-files-section...

    4 von 9 13.06.15 12:36

  • recognizes the subfolder page-templates. Therefore,its a good idea to store your global page templates inthis folder to help keep them organized.

    Top

    Creating Custom Page Templates forGlobal Use #Sometimes youll want a template that can beused globally by any page, or by multiple pages. Somedevelopers will group their templates with a lenameprex, such as page_two-columns.php

    For information on theme le-naming conventions andlenames you cannot use, see reserved themelenames.

    To create a global template, write an opening PHP

    Alert: A specialized page template le(those created for only one time use)cannot be in a sub-folder, nor, if usinga Child Theme, in the Parent Themesfolder.

    Alert: Important! Do not use page- asa prex, as WordPress will interpret thele as a specialized template, meant toapply to only one page on your site.

    Tip: A quick, safe method for creating anew page template is to make a copyof page.php and give the new le adistinct lename. That way, you start owith the HTML structure of your otherpages and you can edit the new le asneeded.

    WordPress Page Templates | Theme Developer Handbook | ... https://developer.wordpress.org/themes/template-files-section...

    5 von 9 13.06.15 12:36

  • comment at the top of the le that states thetemplates name.

    Its a good idea to choose a name that describes whatthe template does as the name is visible to WordPressusers when they are editing the page. For example,you could name your template Homepage, Blog, orPortfolio.

    This example from the TwentyFourteen theme createsa page template called Full Width Page:

    Once you upload the leto your themes folder(e.g., page-templates), goto the Page >Edit screen in your admindashboard.

    On the right hand sideunder attributes youllsee template. This iswhere users are able to

    access your global page templates.

    Top

    Creating a Custom Page Template forOne Specific Page #

    1

  • As mentioned in the Template Hierarchy page, you cancreate a template for a specic page. To create atemplate for one specic page, copy your existingpage.php le and rename it with your pages slug orID:

    page-{slug}.php1. page-{ID}.php2.

    For example: Your About page has a slug of about andan ID of 6. If your active themes folder has a lenamed page-about.php or page-6.php, thenWordPress will automatically nd and use that le torender the About page.

    To be used, specialized page templates must be inyour themes folder (i.e. /wp-content/themes/my-theme-name/ ).

    Top

    Using Conditional Tags in PageTemplates #You can make smaller, page-specic changeswith Conditional Tags in your themes page.php le.For instance, the below example code loads the leheader-home.php for your front page, but loadsanother le (header-about.php) for your Aboutpage, and then applies the default header.php for allother pages.

    You can learn more about Conditional Tags here.

    Top

    Identifying a Page Template #

    1234567

    if ( is_front_page() ) :get_header( 'home' );

    elseif ( is_page( 'About' ) ) :get_header( 'about' );

    else:get_header();

    endif;

    WordPress Page Templates | Theme Developer Handbook | ... https://developer.wordpress.org/themes/template-files-section...

    7 von 9 13.06.15 12:36

  • If your template uses the body_class() function,WordPress will print classes in the body tag for thepost type class name (page), the pages ID (page-id-{ID}), and the page template used. For thedefault page.php, the class name generatedis page-template-default:

    When using a custom page template, theclass page-template will print, along with a classnaming the specic template. For example, if yourcustom page template le is named as follows:

    Then then rendered HTML generated will be asfollows:

    Notice the page-template-my-custom-page-php class that is applied to the body tag.

    Top

    Page Template Functions #These built-in WordPress functions and methods canhelp you work with page templates:

    get_page_template() returns the path of thepage template used to render the page.wp_get_theme()->get_page_templates() returnsall custom page templates available to thecurrently active theme(get_page_templates() is a method of theWP_Theme class).

    1

  • Attachment Template Files

    is_page_template() returns true or falsedepending on whether a custom page templatewas used to render the page.get_page_template_slug() returns the valueof custom eld_wp_page_template (null when the value isempty or default).If a page has been assigneda custom template, the lename of thattemplate is stored as the value of a customeld named '_wp_page_template' (inthe wp_postmeta database table). (Customelds starting with an underscore do not displayin the edit screens custom elds module.)

    Page Template Files

    About

    Blog

    Hosting

    Jobs

    Support

    Developers

    Get Involved

    Learn

    Showcase

    Plugins

    Themes

    Ideas

    WordCamp

    WordPress.TV

    BuddyPress

    bbPress

    WordPress.com

    Matt

    Privacy

    License / GPLv2

    Follow Follow @WordPress@WordPress

    WordPress Page Templates | Theme Developer Handbook | ... https://developer.wordpress.org/themes/template-files-section...

    9 von 9 13.06.15 12:36