• Resolved gulliver

    (@gulliver)


    If in a template I’m including a php file which is in the main theme directory, is there a reason to use ‘get_template_part’ rather than ‘include’? (Other than ‘get_template_part’ fails silently.)

    I can’t fully understand ‘get_template_part’, so haven’t been able to use it and instead use ‘include(locate_template())’.

    As an example, how would I use ‘get_template_part’ to include a template of ‘part-view-all-posts.php’?

Viewing 6 replies - 1 through 6 (of 6 total)
  • get_template_part actually uses include itself. However, where it has an advantage is in making it more flexible – for example, for child themes. If you’re doing this theme for your own purpose only then an include should be fine, otherwise I’d use the template_part feature.

    Thread Starter gulliver

    (@gulliver)

    @dartiss… thanks.

    I updated the initial post since your reply.

    I think I now understand, and can use:

    <?php get_template_part( 'part', 'view-all-posts' ); ?>

    At least I now know how to use it, although I’m still unsure if this is sensible way… my template files have various part-page elements for which the name begins with ‘part-‘.

    Yes, sorry, looks like I’m answering a different question now 😉

    The codex page gives a pretty good explanation of use, although I’m always a big fan of just grabbing an existing theme and working things out from that

    https://developer.wordpress.org/reference/functions/get_template_part/

    This also gives the source for get_template_part which helps explain what it does above and beyond a simple includes – as I said, it uses a function to locate the template part, which includes looking for child themes.

    Thread Starter gulliver

    (@gulliver)

    Thanks.
    I’m also in the habit of ‘grabbing an existing…’ – although often with no real understanding of what I’m doing – hence my regular appearances here asking for help.
    🙂

    Yes. In that case, Codex is your friend. Failing that, Google 😉

    If you believe you’ve now sorted this, could you set the status to “resolved”?

    Thanks.

    Thread Starter gulliver

    (@gulliver)

    Thanks.

Viewing 6 replies - 1 through 6 (of 6 total)

The topic ‘Includes and 'get_template_part'.’ is closed to new replies.