Ato*_*cus 51 wordpress templates
简而言之,我所需要的就是让我的WordPress做到这一点
$var = get_template_part( 'loop', 'index' );
Run Code Online (Sandbox Code Playgroud)
但是,get_template_part()不会返回HTML,它会打印出来.
我需要存储这个HTML $var- 你有什么想法怎么做吗?
Sim*_*rfe 77
这不是什么get_template_part,get_template_part基本上像PHP的require函数.Justin Tadlock在这里写了很多关于这个的内容,还讨论了一个对你来说可能更有用的Wordpress函数 - locate_template.
或者,如果您确实想使用get_template_part破解此功能,则可以使用模板缓冲:
function load_template_part($template_name, $part_name=null) {
ob_start();
get_template_part($template_name, $part_name);
$var = ob_get_contents();
ob_end_clean();
return $var;
}
Run Code Online (Sandbox Code Playgroud)
Tom*_*ger 12
我不喜欢输出缓冲,虽然+1甚至认为这是一个选项!
我认为Helga对某些东西有所了解,但你仍然需要尊重child_themes和主题路径,所以请locate_template()改用(也像Simon建议的那样).
这很好用,甚至可以在过滤器或(在我的情况下)短代码函数中使用(我希望我的短代码在模板样式文件中输出内容,将显示层与逻辑层分开).
return file_get_contents(locate_template("template-file-name.php")); // don't forget the .php!
Run Code Online (Sandbox Code Playgroud)