WordPress.org

Ready to get started?Download WordPress

Forums

Template Part vs Include and Naming Conventions (4 posts)

  1. mattmikulla
    Member
    Posted 2 years ago #

    I am working on my own theme and am beginning to use template parts.

    What I'm a little confused about is when to use template parts or includes.

    For example, I have a chunk of code that displays social sharing widgets. Should that be an include or a template part?

    Also, what would I name that file according to best practices if there are any?

    All the examples I have found always reference the loop index wordpress template part. I get that example for overriding child theme parts blah blah blah.

    Thoughts?

  2. Digital Raindrops
    Member
    Posted 2 years ago #

    Hi,
    This is what I do, not saying it is right or wrong, just that it works for me!

    I write child themes for tutorials and use template parts extensivly, great for adding to existing files, example would be adding two new menu areas to a twenty eleven child theme.

    I use include for functions and setup pages, example would be this child theme which has the Woo Themes FlexSlider, the slider code is in an /inc/ or /flex-slider/ folder and called from functions.php, the slider is 'loaded' via a template part.

    For example, I have a chunk of code that displays social sharing widgets. Should that be an include or a template part?

    I would use a Template Part, if a file was deleted or renamed by mistake, include will give a fatal error unless coded for if( file_exists('filename') ), a template part will just ignore, a template part can also exist in a parent and or child theme and WordPress will find and load it, by priority.

    Naming conventions:
    What I tend to do is to name the files, so I know what they are, lets use the 'social media' as an example, I have done this and used two files.

    Anyone reading my code would know what these files and calls were:

    social-horizontal.php and social-vertical.php

    <?php get_template_part('social','horizontal'); ?>
    
    <?php get_template_part('social','vertical'); ?>

    I also wrote a post last year when I discovered how powerfull the template parts are giving some reasons, not the best written post but it might help.

    HTH

    David

  3. mattmikulla
    Member
    Posted 2 years ago #

    Hey David. Thank you so much for your insight.

    I actually read the last article (Ten Good Reasons...) yesterday when doing some research! Reading again and bookmarking.

    Another Question: Do you ever put your template parts in a separate directory as you do with includes? Or, will that mess up the functionality of template parts and inheritance?

  4. Digital Raindrops
    Member
    Posted 2 years ago #

    I put them in the themes folder, with a good naming convention they are easy to locate.

    I have not tried to put them into a folder and not sure if WordPress will look into sub folders without adding a filter.

    I did something with a Facebook template a while back and added a filter, but I cannot locate the function at the moment.

    HTH

    David

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.