WordPress.org

Ready to get started?Download WordPress

Forums

Include template parts with PHP (4 posts)

  1. theodorlindekaer
    Member
    Posted 1 year ago #

    I see that many themes use:

    get_header()
    get_footer()

    When I create a now part (fx banner.php) the following does not work:

    get_banner()

    Instead I have to use:

    get_template_part('banner')

    My question is: why is get_header used and not just consistently get_template_part('header')? What is the purpose of this GET function?

    Thank you!

  2. paulwpxp
    Font hero
    Posted 1 year ago #

    To load a php file in your own theme, use locate_template()
    http://codex.wordpress.org/Function_Reference/locate_template

    The get_template_part(), get_header(), and such all make use of locate_template() with a systematic order of slugs.

    See wp-includes/general-template.php for more details.

  3. theodorlindekaer
    Member
    Posted 1 year ago #

    when I use:

    locate_template('banner') I am not able to fetch my own banner.php.

    When I use:

    get_template_part('banner') it works great.

    Hmmmm...

  4. paulwpxp
    Font hero
    Posted 1 year ago #

    The first parameter is array, also the second must be true for it to load ( see the doc in the link above )

    locate_template( array( 'banner.php' ), true, false );

    There is nothing wrong with get_template_part() though, it has more fancy loading order based on slugs. I would just use locate_template() because most of the time when loading special php file in theme ( like the splash banner in front page for example ) won't be needing that fancy checking.

Topic Closed

This topic has been closed to new replies.

About this Topic