WordPress.org

Support

Support » How-To and Troubleshooting » Why We Do Get Template Part

Why We Do Get Template Part

  • Why do we do <?php get_template_part($slug, $name); ?>?
    Is it just a naming thing?
    Lets say I wanted to make different types of single files, like single-media, single-sidebar.
    These are all files I will call on the single page.
    Is this just a naming thing? is there any use for the ‘slug’?

Viewing 7 replies - 1 through 7 (of 7 total)
  • WPyogi

    @wpyogi

    Forum Moderator

    I read that. Still a bit confused

    Moderator Jan Dembowski

    @jdembowski

    Volunteer Mod. & Brute Squad

    Is this just a naming thing? is there any use for the ‘slug’?

    It’s just a useful naming convention and really works well for themes that want to be child theme friendly.

    For some examples and documentation on when/where to use get_template_part() give Chip’s presentation a read.

    Make sure you have coffee before you start. I think the parts you want are around slide 60. 😉

    Ahhh, makes so much sense
    Thanks

    WPyogi

    @wpyogi

    Forum Moderator

    Moderator Jan Dembowski

    @jdembowski

    Volunteer Mod. & Brute Squad

    Those are useful links too, and I’m going to bask in the momentary glory of beating anyone to a reply (while looking around to see if Esmi or alchymyth beat me too as they always do with better replies…) 😉

    Chip Bennett

    @chipbennett

    Theme Review Admin

    The get_template_part() template tag is simply a file-include call to include any arbitrarily named file. It is analogous to get_header() to include header.php, get_footer() to include footer.php, get_sidebar() to include sidebar.php(), andget_search_form()to includesearchform.php`.

    Calling get_template_part( $slug ) will include a file named <strong>$slug.php</strong>. The most common use case is get_template_part( 'loop' ) to include loop.php, as a means of abstracting the loop code out of the main template files.

    Calling get_template_part( $slug, $name ) will include a file named $slug-$name.php. So continuing our example above, you could have a different loop output for different post formats (video, image, gallery, aside, etc.). Combining get_template_part() with get_post_format() yields a very powerful way to include post-format-specific loop output, via get_template_part( 'loop', get_post_format() ). Using that call, if the current post has a post format of “gallery”, the loop-gallery.php file would be included automatically.

    But here’s the fun part: get_template_part() is really just a fancy wrapper for locate_template(): meaning that it is more powerful than a simple include() call, because it has the built-in ability to fall back to less-specific files.

    If you are using a Child Theme, and call get_template_part( $slug, $name ), WordPress will attempt to include each of the following files, in order:

    * $slug-$name.php in the <em>Child</em> Theme
    * $slug-$name.php in the <em>Parent</em> Theme
    * $slug.php in the <em>Child</em> Theme
    * $slug.php in the <em>Parent</em> Theme

    So if your parent Theme includes loop.php, you can override that file just for posts that have the “gallery” post format, simply by including loop-gallery.php in your Child Theme, and ensuring that you call get_template_part( 'loop', get_post_format() ) in the appropriate place(s).

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Why We Do Get Template Part’ is closed to new replies.