Codex tools: Log in
Languages: English • 日本語 • 中文(简体) • Português do Brasil • (Add your language)
Contents |
Load a template part into a template (other than header, sidebar, footer). Makes it easy for a theme to reuse sections of code and an easy way for child themes to replace sections of their parent theme.
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".
<?php get_template_part( $slug, $name ); ?>
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 first file that exists among these, in this priority:
To use this function with subfolders in your theme directory, simply prepend the folder name before the slug. For example, if you have a folder called "partials" in your theme directory and a template part called "content-page.php" in that sub-folder, you would use get_template_part() like this:
<?php get_template_part( 'partials/content', 'page' ); ?>
Adding a navigation bar to theme using a generic 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) ?>
get_template_part() is located in wp-includes/general-template.php.
Include Tags: get_header(), get_footer(), get_sidebar(), get_template_part(), get_search_form(), comments_template()