admin管理员组文章数量:1130349
function custom_code(){
return include( get_stylesheet_directory() . '/custom-template.php');
}
add_shortcode('custom_shortcode','custom_code');
As you can check above I want to include a template using shortcode how to make it done?
function custom_code(){
return include( get_stylesheet_directory() . '/custom-template.php');
}
add_shortcode('custom_shortcode','custom_code');
As you can check above I want to include a template using shortcode how to make it done?
Share Improve this question asked Jan 7, 2019 at 9:10 Dharit SoniDharit Soni 1132 silver badges6 bronze badges 1- Please check once gist.github/germanny/9420496 – Pratik Patel Commented Jan 7, 2019 at 9:20
1 Answer
Reset to default 1You can't return the include statement. And you shouldn't use include template parts.
And here's how to do it correctly:
function custom_code() {
ob_start();
get_template_part( 'custom-template.php' );
return ob_get_clean();
}
add_shortcode( 'custom_shortcode', 'custom_code' );
So what we're doing here is:
- Start output buffering.
- Include given template part using
get_template_partfunction. - Return all the output generated by that template and delete the buffer.
function custom_code(){
return include( get_stylesheet_directory() . '/custom-template.php');
}
add_shortcode('custom_shortcode','custom_code');
As you can check above I want to include a template using shortcode how to make it done?
function custom_code(){
return include( get_stylesheet_directory() . '/custom-template.php');
}
add_shortcode('custom_shortcode','custom_code');
As you can check above I want to include a template using shortcode how to make it done?
Share Improve this question asked Jan 7, 2019 at 9:10 Dharit SoniDharit Soni 1132 silver badges6 bronze badges 1- Please check once gist.github/germanny/9420496 – Pratik Patel Commented Jan 7, 2019 at 9:20
1 Answer
Reset to default 1You can't return the include statement. And you shouldn't use include template parts.
And here's how to do it correctly:
function custom_code() {
ob_start();
get_template_part( 'custom-template.php' );
return ob_get_clean();
}
add_shortcode( 'custom_shortcode', 'custom_code' );
So what we're doing here is:
- Start output buffering.
- Include given template part using
get_template_partfunction. - Return all the output generated by that template and delete the buffer.
本文标签: functionsHow to include any template using Shortcode fuction
版权声明:本文标题:functions - How to include any template using Shortcode fuction? 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749036536a2306477.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论