2

I'm creating my own theme framework, and I want to organize my code, separating the partials files from the pages.

But I want to do that and still use the native functions get_header(), get_footer() and get_sidebar().

How can I change this functions to look for the templates on the directory partials/ instead of the theme's root directory?

1 Answer 1

3

Simple solution, use get_template_part().

For example:

get_template_part( 'partials/footer' ); 

Which would get the footer.php inside the partials/ directory.

Another example:

get_template_part( 'partials/footer', 'home' ); 

Which would get the footer-home.php inside the partials/ directory.

One more example:

get_template_part( 'partials/footer/blog' ); 

Which would get the blog.php inside the partials/footer/ directory.

3
  • It works fine, thanks! But I got a problem with WooCommerce. The problem is that WooCommerce uses get_header/sidebar/footer on their templates, and since I change the directory of those files, WP gives me error that says that theme without header/footer/sidebar is obsolete since version 3.0. Do you know how I can make this work? Jul 27, 2015 at 14:41
  • If you implement a custom template structure, then you should do the same with the WooCommerce templates by customizing them too. Unfortunately get_footer() isn't very customizable here and if there is some smart solution, then I don't know it. The problem lies in the usage of get_footer() - and get_header/get_sidebar - itself, because it checks for the existence of the file in the theme root and if not available gives you the error and loads wp-includes/theme-compat/footer.php. @vinigarcia87 Jul 27, 2015 at 15:26
  • It's sad that we can't just hook the defaulf wp functions. :S Thanks for your answer, was very helpful! Jul 28, 2015 at 2:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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