0

Suppose their is a wordpress theme say zumper. Apart from files such as footer.php, header.php their are other folders such as layouts inc

suppose I have a file america.php in layouts folder and york.php in inc folder

what is the correct path and correct way to include york.php from inc folder to america.php, which is in layout folder

Summarizing - america.php - Layout Folder york.php - inc folder

I want to include york.php in america.php what is the correct way specially the correct path?

1

Use:

get_template_directory() and or get_stylesheet_directory()

E.g. in zumper/layouts/america.php

<?php

include get_template_directory() . '/inc/york.php';
//etc...

Note:

get_template_directory

Retrieves the absolute path to the directory of the current theme.

get_stylesheet_directory

Retrieve stylesheet directory Path for the current theme/child theme.

So if your theme is a child theme and or you want to provide the ability for a child theme to override your parent include then use the latter.

| improve this answer | |
  • It worked. i was using something like this - <?php get_template_part('/inc/york.php'); ?> – The WP Intermediate Jan 26 '16 at 10:02
  • Done. Accepted. – The WP Intermediate Jan 26 '16 at 10:34
  • get_template_part would also work but in your use case you need to drop the .php extension off the argument as the extension gets appended in the function itself. get_template_part('inc/york') - also get_template_part() first searches for a child theme before looking in the parent theme, so it's good to use that when you want to ensure child themes can override your parent theme file. It is acceptable to use get_template_part() in your use case also. – Adam Jan 26 '16 at 11:34
  • Ok sir.I will try with that Option also. – The WP Intermediate Jan 26 '16 at 13:39

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.