Theme Partial Shortcode
This post may contain affiliate links, which means I may receive a commission if you click a link and purchase something that I have recommended. Thank you!
Have you ever been building a website using Divi, Elementor, or any of the other popular page builder tools and wished that you could just add some custom PHP without needing to go through the whole process of creating a custom module? Just me? Well, there are probably other uses for this snippet as well I guess. haha
I use a snippet similar to this in place of custom modules in much of my development.
The following code snippet will allow you to add everything from custom queries, headers, or anything you can think of to any part of any page that supports shortcodes.
We start with creating the shortcode.
/************
* Theme Partials Shortcode
* -----------------
* Built the navigation as a partial so that updates to Divi won't break.
* This keeps us out of the header.php file and instead use the Theme Builder tool from Divi
* Use [partial name='header']
* *******************/
add_shortcode('partial', 'template_partial');
function template_partial($atts) {
extract(shortcode_atts(array(
'dir' => 'partials',
'name' => FALSE
), $atts));
if ( $name ) {
ob_start();
get_template_part( $dir . '/' . $name );
return ob_get_clean();
}
}