Skip to content

Instantly share code, notes, and snippets.

@mrwweb
Created August 26, 2022 15:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrwweb/0e9730ec84eb616fe0e05746d7b5cfce to your computer and use it in GitHub Desktop.
Save mrwweb/0e9730ec84eb616fe0e05746d7b5cfce to your computer and use it in GitHub Desktop.
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