Last active
April 29, 2025 15:11
-
-
Save cccamuseme/514ecbd685341bf5529bb68fa04fbf50 to your computer and use it in GitHub Desktop.
Create Wordpress shortcode to get template part
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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