So in my index.php
I called get_header()
, get_sidebar()
and get_footer()
. My index.php
is kind of huge. It is separated into 5 divs more. How do I make it cleaner? Is it safe to chop it to files and include the files?
-
1why don't you just give it "a chop" first?– JesseApr 24, 2013 at 3:47
1 Answer
Yes, you can separate your code and markup into multiple files. Be aware of scope issues when you use include, but done carefully it should be no problem. Since you are in WordPress and building a theme, I would suggest get_template_part
over a raw include
.
In many ways, "chopping" up your theme files is what get_template_part
is for-- well, at least it is very convenient for that. It lets you separate components of the page and include them in a way that makes easy to extend via child themes.
-
-
@Zlatev : the question asked about
include
so I talked aboutinclude
. Yes,get_template_part
useslocate_template
which usesload_template
which does userequire
, orrequire_once
. Given thatlocate_template
locates the file before trying to load the file I don't see the significant difference between the two in context.require
will kill the script,include
won't, but given that the function never runs unless the file is located it doesn't really matter. You canget_template_part('nonexistent','template');
all you want and the script just skips right over it.– s_ha_dumApr 24, 2013 at 13:38 -
Of course. I simply wanted to clarify what QA can expect using
get_template_part
as included files won't inherit variables for example. Apr 24, 2013 at 19:49