Skip to content

Instantly share code, notes, and snippets.

@cccamuseme
Last active April 29, 2025 15:11
Show Gist options
  • Save cccamuseme/514ecbd685341bf5529bb68fa04fbf50 to your computer and use it in GitHub Desktop.
Save cccamuseme/514ecbd685341bf5529bb68fa04fbf50 to your computer and use it in GitHub Desktop.
Create Wordpress shortcode to get template part
// Add this code to your theme’s functions.php file:
function my_custom_shortcode() {
ob_start(); // Start output buffering
get_template_part('template-parts/my-template'); // Load the template part
return ob_get_clean(); // Return the buffered output
}
add_shortcode('myshortcode', 'my_custom_shortcode');
//This registers [myshortcode] which loads template-parts/my-template.php.
// Create the file inside your theme: /wp-content/themes/your-theme/template-parts/my-template.php
// Add this in the WordPress editor (Classic or Block): [myshortcode]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment