Can I Get The Template
Page Templates
Topics
- Uses for Page Templates
- Page Templates inside the Template Hierarchy
- Page Templates Purpose & User Control
- File Organization of Page Templates
- Creating Custom Folio Templates for Global Utilise
- Creating a Custom Folio Template for One Specific Page
- Creating folio templates for specific post types
- Using Provisional Tags in Page Templates
- Identifying a Page Template
- Page Template Functions
Folio templates are a specific blazon of template file that can be applied to a specific folio or groups of pages.
Note: Every bit of WordPress 4.7 page templates support all post types. For more details how to gear up a folio template to specific post types see example beneath.
Since a page template is a specific blazon of template file, here are some distinguishing features of page templates:
- Folio templates are used to change the look and feel of a folio.
- A page template can be applied to a single page, a page section, or a form of pages.
- Page templates by and large have a loftier level of specificity, targeting an individual folio or group of pages. For example, a folio template named
page-virtually.php
is more than specific than the template filespage.php
oralphabetize.php
as it will only affect a folio with the slug of "virtually." - If a page template has a template proper name, WordPress users editing the page have control over what template will be used to render the page.
Uses for Folio Templates
Page templates display your site's dynamic content on a page, e.thousand., posts, news updates, calendar events, media files, etc. You lot may decide that yous want your homepage to await a specific way, that is quite unlike to other parts of your site. Or, y'all may want to brandish a featured image that links to a post on one part of the page, have a list of latest posts elsewhere, and use a custom navigation. Yous can employ folio templates to achieve these things.
This section shows you lot how to build folio templates that can be selected by your users through their admin screens.
For example, you tin build page templates for:
- full-width, i-column
- two-cavalcade with a sidebar on the right
- two-cavalcade with a sidebar on the left
- three-column
Top ↑
Page Templates inside the Template Hierarchy
When a person browses to your website, WordPress selects which template to utilise for rendering that page. As we learned before in the Template Hierarchy, WordPress looks for template files in the following club:
- Folio Template — If the page has a custom template assigned, WordPress looks for that file and, if constitute, uses it.
-
page-{slug}.php
— If no custom template has been assigned, WordPress looks for and uses a specialized template that contains the page's slug. -
folio-{id}.php
— If a specialized template that includes the page's slug is not institute, WordPress looks for and uses a specialized template named with the page's ID. -
page.php
— If a specialized template that includes the page's ID is not found, WordPress looks for and uses the theme's default page template. -
singular.php
— Ifpage.php
is not found, WordPress looks for and uses the theme's template used for a single post, irregardless of mail blazon. -
index.php
— If no specific page templates are assigned or plant, WordPress defaults dorsum to using the theme's index file to render pages.
Alert: There is also a WordPress-defined template namedpaged.php
. It isnot used for the page post-type but rather for displaying multiple pages of archives.
Top ↑
Page Templates Purpose & User Control
If you program on making a custom page template for your theme, you should decide a couple of things before proceeding:
- Whether the page template will exist for one specific page or for any folio; and
- What type of user command yous want bachelor for the template.
Every page template that has a template name can exist selected by a user when they create or edit a page. The list of bachelor templates can be plant atPages > Add together New > Attributes > Template. Therefore, a WordPress user can choose whatever page template with a template name, which might non be your intention.
For example, if you want to have a specific template for your "About" page, it might not be appropriate to name that page template "Almost Template" as it would exist globally available to all pages (i.e. the user could use it to any page). Instead, create a single use template and WordPress will return the page with the advisable template, whenever a user visits the "Most" page.
Conversely, many themes include the power to choose how many columns a folio will have. Each of these options is a page template that is available globally. To give your WordPress users this global choice, you will need to create folio templates for each option and give each a template name.
Dictating whether a template is for global utilise vs. singular use is achieved by the way the file is named and whether or not is has a specific comment.
Annotation: Sometimes information technology is appropriate to have a template globally available fifty-fifty if it appears to be a single employ case. When y'all're creating themes for release, it can exist hard to predict what a user will name their pages. Portfolio pages are a peachy example equally not every WordPress user will name their portfolio the same affair or have the same page ID and however they may want to apply that template.
Elevation ↑
File Organization of Folio Templates
Equally discussed in Organizing Theme Files, WordPress recognizes the subfolder page-templates. Therefore, it's a adept idea to shop your global page templates in this folder to help continue them organized.
Warning: A specialized page template file (those created for simply one time use) cannot be in a sub-folder, nor, if using a Child Theme, in the Parent Theme'south binder.
Superlative ↑
Creating Custom Page Templates for Global Apply
Sometimes you'll want a template that can be used globally by any page, or by multiple pages. Some developers volition group their templates with a filename prefix, such aspage_two-columns.php
Alarm: Important! Exercise not use page-
as a prefix, equally WordPress volition interpret the file as a specialized template, meant to utilise to simply 1 page on your site.
For information on theme file-naming conventions and filenames yous cannot utilise, see reserved theme filenames.
Tip: A quick, safe method for creating a new page template is to make a re-create ofpage.php
and requite the new file a distinct filename. That style, you showtime off with the HTML structure of your other pages and you can edit the new file as needed.
To create a global template, write an opening PHP comment at the top of the file that states the template'southward proper noun.
<?php /* Template Proper noun: Instance Template */ ?>
It's a expert idea to cull a name that describes what the template does as the name is visible to WordPress users when they are editing the page. For case, you could name your template Homepage, Blog, or Portfolio.
This example from the TwentyFourteen theme creates a page template chosen Full Width Page:
<?php /** * Template Name: Full Width Folio * * @package WordPress * @subpackage Twenty_Fourteen * @since Twenty 14 1.0 */
One time yous upload the file to your theme'south folder (e.one thousand., page-templates), go to thePage > Edit screen in your admin dashboard.
On the correct hand side underattributesyou'll see template. This is where users are able to access your global page templates.
Tip: The select list has a maximum width of 250px, so longer names may be cut off.
Elevation ↑
Creating a Custom Page Template for One Specific Page
Equally mentioned in the Template Hierarchy page, y'all can create a template for a specific page. To create a template for one specific page, copy your existing page.php
file and rename it with your page's slug or ID:
-
page-{slug}.php
-
page-{ID}.php
For instance: Your About page has a slug of 'nigh' and an ID of 6. If your active theme's folder has a file namedpage-about.php
orpage-six.php
, then WordPress will automatically find and apply that file to render the Most page.
To be used, specialized page templates must be in your theme's binder (i.east./wp-content/themes/my-theme-name/ ).
Pinnacle ↑
Creating page templates for specific post types
By default, a custom folio template will exist available to the "page" post type.
To create a page template to specific postal service types, add a line under the template proper name with the mail types you would like the template to support.
Example:
<?php /* Template Name: Full-width layout Template Post Blazon: mail service, page, event */ // Page lawmaking here...
Alert: This power to add together page templates to mail service types other than "page" post type is supported only from WordPress 4.vii
When at least one template exists for a post type, the 'Post Attributes' meta box will be displayed in the back end, without the need to add post type back up for 'page-attributes' or anything else. The 'Mail Attributes' label can be customzied per mail service type using the 'attributes' label when registering a mail blazon.
Backward Compatibility:
Allow's say you desire to publicly release a theme with back up for postal service blazon templates. WordPress versions earlier 4.vii will ignore the Template Post Type header and evidence the template in the listing of folio templates, even though it only works for regular posts. To prevent that, you can hook into the theme_page_templates filter to exclude it from the list. Here's an example:
/** * Hides the custom mail service template for pages on WordPress 4.6 and older * * @param array $post_templates Assortment of folio templates. Keys are filenames, values are translated names. * @return array Filtered assortment of page templates. */ function makewp_exclude_page_templates( $post_templates ) { if ( version_compare( $GLOBALS['wp_version'], 'iv.7', '<' ) ) { unset( $post_templates['templates/my-total-width-post-template.php'] ); } return $post_templates; } add_filter( 'theme_page_templates', 'makewp_exclude_page_templates' );
Aggrandize full source codeCollapse full source code
That style you can support custom postal service type templates in WordPress 4.vii and beyond while maintaining total backward compatibility.
Note that theme_page_templates is actually a dynamic theme_{$post_type}_templates filter. The dynamic portion of the hook proper noun, $post_type, refers to the mail service type supported by the templates. Due east.g. yous can hook into theme_product_templates to filter the list of templates for the product post type.
Acme ↑
Using Provisional Tags in Page Templates
You can brand smaller, page-specific changes with Conditional Tags in your theme'spage.php
file. For example, the below example code loads the file header-domicile.php
for your front page, but loads another file (header-about.php
) for your Most page, and then applies the defaultheader.php
for all other pages.
if ( is_front_page() ) : get_header( 'home' ); elseif ( is_page( 'About' ) ) : get_header( 'about' ); else: get_header(); endif;
You can larn more about Conditional Tags here.
Top ↑
Identifying a Folio Template
If your template uses thebody_class()
part, WordPress will print classes in thebody
tag for the mail service type class proper noun (page
), the page's ID (page-id-{ID}
), and the page template used. For the defaultfolio.php
, the course proper noun generated ispage-template-default
:
<body grade="folio page-id-6 folio-template-default">
Note: A specialized template (page-{slug}.php
orpage-{ID}.php
) too gets thepage-template-default
grade rather than its own torso class.
When using a custom folio template, the classpage-template
volition print, forth with a grade naming the specific template. For example, if your custom page template file is named as follows:
<?php /* Template Name: My Custom Page */ ?gt;
Then then rendered HTML generated will be as follows:
<body form="folio page-id-6 page-template folio-template-my-custom-folio-php">
Notice the page-template-my-custom-page-php
class that is practical to the body
tag.
Top ↑
Folio Template Functions
These congenital-in WordPress functions and methods can help you work with page templates:
-
get_page_template()
returns the path of the page template used to render the page. -
wp_get_theme()->get_page_templates()
returns all custom page templates available to the currently active theme (get_page_templates()
is a method of theWP_Theme
grade). -
is_page_template()
returns truthful or faux depending on whether a custom folio template was used to render the page. -
get_page_template_slug()
returns the value of custom field_wp_page_template
(null
when the value is empty or "default").If a page has been assigned a custom template, the filename of that template is stored equally the value of a custom field named'_wp_page_template'
(in thewp_postmeta
database tabular array). (Custom fields starting with an underscore do not display in the edit screen'southward custom fields module.)
Can I Get The Template,
Source: https://developer.wordpress.org/themes/template-files-section/page-template-files/
Posted by: grahamsatchis.blogspot.com
0 Response to "Can I Get The Template"
Post a Comment