Support » Themes and Templates » Benefit of get_template_parts

  • Resolved imagebox

    (@imagebox)


    I recently downloaded the new Starkers v4 theme. I noticed that in lieu of using <?php get_header(); ?> to include the header code

    They’re using <?php get_template_parts( array( 'parts/shared/html-header', 'parts/shared/header' ) ); ?> and by which including 2 separate files /parts/shared/header.php and /parts/shared/header-html.php

    I am just curious if there is any benefit to doing it this way? I would normally just create a header.php file and put in the root theme folder and call it using:

    <?php get_header(); ?>

    Can someone give me a reason(s) to take this approach. It sort looks like they were trying to take more of a MVC approach to the theme code.

Viewing 4 replies - 1 through 4 (of 4 total)
  • Max

    (@clementsm)

    I don’t think there is a function called “get_template_parts()” in WordPress, so thats a custom function they have written. THe wordpress one is called get_template_part and does not take an array as a parameter.

    They are probably using it to have some special include behaviour for different templates in the theme.

    imagebox

    (@imagebox)

    Awww…that makes sense. My thought is they created that function to accept the array to easily allow certain pages to have a unique header or footer include to allow for scripts or some other code which should only appear on a specific page. Rather than doing a

    if(is_home()){

    test in the header.php file you could simply include a separate file with all of the html or logic in it.

    Hi there, I was just looking at the new Starkers v4 theme too and wondered the same thing, checked codex but only showing get_template_part

    Im still not quiet ure what it is doing, I had a look at both files <?php get_template_parts( array( 'parts/shared/html-header', 'parts/shared/header' ) ); ?> that it was calling and it would seem that it allows to load the two files, one after the other?

    Ops just read this too : Please see /external/starkers-utilities.php for info on get_template_parts()

    Simple wrapper for native get_template_part()
    Allows you to pass in an array of parts and output them in your theme
    e.g. <?php get_template_parts(array(‘part-1’, ‘part-2’)); ?>

    Simple wrapper for native get_template_part()
    Allows you to pass in an array of parts and output them in your theme
    e.g. <?php get_template_parts(array(‘part-1’, ‘part-2’)); ?>

    Too bad it’s not working though. It keeps going with the native, no matter what you add there.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Benefit of get_template_parts’ is closed to new replies.