0

My goal is to render one identical element on every page. At the moment this element is manually added to 50+ pages. I saved it as a template named 'reusable' and then i want to call it from functions.php using a hook. Is that possible (is this a valid use case) and if so, how? My code, at the very end of functions.php, would look like:

add_action('get_footer', 'render_reusable');
function render_reusable() {
    return how_can_i_load_template_component_named('reusable');
}

1 Answer 1

1

You can create a template tag in your functions.php file like so:

if ( ! function_exists( 'mytheme_reusable' ) ) :
     function mytheme_reusable() { 
           //put your HTML or whatever code you need here
     }
endif;

You can then simply apply the template tag in any template exactly where you need it:

<?php mytheme_reusable(); ?>

Alternatively you can also then apply it via add_action, like so:

add_action( 'wp_footer', 'render_reusable', 1 );
function render_reusable() {
    mytheme_reusable();
}

get_footer(); is intended to include your footer.php content. I set the priority as 1 assuming you wanted it to load before the scripts. Question though, if you're loading this on tons of pages then you may just want to put your template tag directly into the footer.php file and then if necessary write some conditions to prevent it from loading on some pages.

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.