0

The parent theme uses this construct to include the navbar:

include(locate_template( "components/navigation.php" ));

I would prefer to use the Wordpress convention:

get_template_part( "components/navigation" );

in my child theme, to call the file from the parent theme, but his code works and mine doesn't. What am I doing wrong?

UPDATE:

The surrounding code looks like this:

if ( $navigation_type != '' ) {
  include(locate_template( "components/navigation.php" ));
}

This code loads fine, the debug log is empty. If I switch that code to

if ( $navigation_type != '' ) {
  get_template_part( 'components/navigation' );
}

then The debug log fills with PHP Notice: Undefined variable: navigation_type in /path/to/components/navigation.php on line 31

  • get_template_part should work the way you use it. activate the debug mode to see if there is an error codex.wordpress.org/Debugging_in_WordPress – mmm Feb 4 '18 at 17:53
  • Debug Queries shows it is being correctly called, but there is no output. Strange, the debug log shows the encapsulating conditional var as undefined if I switch to get_template_part. – Gary D Feb 4 '18 at 19:54
  • 1
    $navigation_type will be undefined in /path/to/components/navigation.php when using get_template_part() because the file is included in the load_template() scope and not the global scope. – Nathan Johnson Feb 4 '18 at 22:30
1

when you use get_template_part, your file navigation.php cannot access variable $navigation_type. but when you do include and locate_template it is simply like including a php file so it can access that variable. It is due to scope of variable in your context.

  • I'd just add that this would probably indicate that the original parent theme isn't so well written. – Andy Macaulay-Brook Apr 5 '18 at 11:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.