How to use get_template_part instead of include_once?

I’m including a file by include_once:

include_once( trailingslashit( get_stylesheet_directory() ) . '/recaptchalib.php' );

However I get warning from theme check plugin:

Read More
INFO: The theme appears to use include or require. 
If these are being used to include separate sections of a template from independent files, 
then get_template_part() should be used instead. 

How can I include the above file using the get_template_part?

Related posts

2 comments

  1. If you take a look at the Codex page for get_template_part(), you fill find the following usage:

    Load a template part into a template (other than header, sidebar, footer). Makes it easy for a theme to reuse sections of code and an easy way for child themes to replace sections of their parent theme.

    Includes the named template part for a theme or if a name is specified then a specialized part will be included. If the theme contains no {slug}.php file then no template will be included.

    <?php get_template_part( $slug, $name ); ?>
    

    $slug
    (string) (required) The slug name for the generic template.
    Default: None

    $name
    (string) (optional) The name of the specialized template.
    Default: None

    That means you can use the following:

    <?php get_template_part( 'recaptchalib' ); ?>
    

    You can use this to include every file in your theme directory.

  2. The template part works like this.
    If you have file in main folder then you can do like this:

    get_template_part('recaptchalib');
    

    In this example the file name is recaptchalib.php.

    and if you have file in any other directory then you should called it by

     get_template_part('templates/layouts', 'recaptchalib');
    

    In this example file is in templates/layouts and file name is recaptchalib.php.

Comments are closed.