Categories

Add Template File to a Post Using Shortcode

// read file
function include_template_file ($template_path=null) {
	if($template_path == null)
		return '';
	ob_start();
    load_template( get_template_directory() .'/'. $template_path );
	$content = ob_get_contents();
	ob_end_clean();
	return $content;
}
 
// shortcode code
function include_template_shortcode($atts, $content=''){
	 extract( shortcode_atts( array(
	      'path' => null
     ), $atts ) );
	return include_template_file($path);
}
 
add_shortcode( 'include_template', 'include_template_shortcode' );
 
// Example:
// [include_template path='assets/my-file.php']

Leave a Reply

 

 

 

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

IMPORTANT! To be able to proceed, you need to solve the following simple math (so I know that you are a human) :-)

What is 12 + 12 ?
Please leave these two fields as-is: