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 "Function Reference/get template part"

(Description)
(Examples)
Line 38: Line 38:
   
 
source: [http://www.wp-tricks.co.il/%D7%94%D7%95%D7%A1%D7%A4%D7%AA-%D7%A7%D7%91%D7%A6%D7%99%D7%9D-%D7%92%D7%A0%D7%A8%D7%99%D7%99%D7%9D-%D7%A0%D7%95%D7%A1%D7%A3-%D7%A2%D7%9C-header-footer-%D7%95-sidebar/ wp-tricks.co.il - using get_template_part() to create generic navigation bar]
 
source: [http://www.wp-tricks.co.il/%D7%94%D7%95%D7%A1%D7%A4%D7%AA-%D7%A7%D7%91%D7%A6%D7%99%D7%9D-%D7%92%D7%A0%D7%A8%D7%99%D7%99%D7%9D-%D7%A0%D7%95%D7%A1%D7%A3-%D7%A2%D7%9C-header-footer-%D7%95-sidebar/ wp-tricks.co.il - using get_template_part() to create generic navigation bar]
  +
  +
=== Storing Parts In A Folder ===
  +
For organizational purposes, you can store your theme parts in a folder inside of your theme. For example:
  +
  +
<?php get_template_part( 'blocks/advertisement' ); ?>
  +
  +
That will attempt to load <code>wp-content/themes/your-theme/blocks/advertisement.php</code>.
   
 
== Notes ==
 
== Notes ==

Revision as of 01:50, 25 August 2010

Description

Load a template part into a template (other than header, sidebar, footer). Makes it easy for a theme to reuse sections of code in an easy to overload way for child themes.

Includes the named template part for a theme or if a name is specified then a specialized part will be included. If the theme contains no {slug}.php file then no template will be included.

For the parameter, if the file is called "{slug}-{name}.php".

Usage

 <?php get_template_part$slug$name ?> 

Parameters

$slug
(string) (required) The slug name for the generic template.
Default: None
$name
(string) (optional) The name of the specialized template.
Default: None

Examples

Using loop.php in child themes

Assuming the theme folder is wp-content/themes, that the parent theme is twentyten, and the child theme is twentytenchild, then the following code --

<?php get_template_part( 'loop', 'index' ); ?>

will do a PHP require() for the named files in this order:

  1. wp-content/themes/twentytenchild/loop-index.php
  2. wp-content/themes/twentytenchild/loop.php
  3. wp-content/themes/twentyten/loop-index.php
  4. wp-content/themes/twentyten/loop.php

Navigation

Adding a navigation bar to theme using a gereric nav.php template file:

<?php get_template_part( 'nav' );           // Navigation bar (nav.php) ?>
<?php get_template_part( 'nav', '2' );      // Navigation bar #2 (nav-2.php) ?>
<?php get_template_part( 'nav', 'single' ); // Navigation bar to use in single pages (nav-single.php) ?>

source: wp-tricks.co.il - using get_template_part() to create generic navigation bar

Storing Parts In A Folder

For organizational purposes, you can store your theme parts in a folder inside of your theme. For example:

<?php get_template_part( 'blocks/advertisement' ); ?>

That will attempt to load wp-content/themes/your-theme/blocks/advertisement.php.

Notes

Change Log

Source File

get_template_part() is located in wp-includes/general-template.php.

Related

Include Tags

See also index of Function Reference and index of Template Tags.