Skip to content

Instantly share code, notes, and snippets.

@mrwweb
Created Aug 26, 2022
Embed
What would you like to do?
Using get_template_part() for Componentized WordPress Themes
<?php
/**
* $args is available with all values passed in third parameter of get_template_part()
*
* In a real theme, this would go in something like parts/button.php or components/button.php
*
* Think carefully about which $args you assume will always be available and which you need to check for
*
* @link https://developer.wordpress.org/reference/functions/get_template_part/
*/
<a class="my-super-sweet-button <?php echo esc_attr( $args['extra_classes'] ?? '' ); ?>"
href="<?php echo esc_url( $args['href'] ); ?>">
<?php echo esc_html( $args['label'] ); ?>
</a>
/**
* Any WordPress Template
*/
<!-- header stuff -->
<?php
get_template_part('parts/button', '', array(
'label' => 'Click me!',
'href' => 'https://example.com',
'extra_classes' => 'custom-class',
) );
?>
<!-- footer stuff -->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment