The php functions get_header() and get_footer() both call the header.php and footer.php files.

Could I create a page like element.php and have a function call it (like get_element()), where I would have it on the pages that I use that function, but not others, or is this not possible? I searched the documentation, but I don't think I know the official name for what I'm looking for - in essence a function that calls an element for some pages.


It sounds like get_template_part() would do the trick.

In element.php you could simply call get_template_part( 'element-item' );, which would then include the element-item.php file from your theme.

You could wrap this up in a function if you wanted to, e.g. get_element().

function get_element() {
    get_template_part( 'element-item' );

This approach would allow you to add logic into the get_element() function so that it only gets the template part under certain conditions and it would keep the element.php template from our example cleaner since the logic would be bundled up in get_element().


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.