<?php get_template_part( \'template-parts/component-page/component-$variable); ?>
在当前有$variable的位置添加变量的正确方法是什么?将变量添加到GET_TEMPLATE_PART
1 个回复
最合适的回答,由SO网友:Dave Romsey 整理而成
看起来您只是想连接一个字符串,所以可以这样做:
<?php get_template_part( \'template-parts/component-page/component-\' . $variable ); ?>
或者可以使用字符串插值(请注意此处使用的双引号):<?php get_template_part( "template-parts/component-page/component-{$variable}" ); ?>
或者,get_template_part()
可以采用两个参数,$slug
和$name
:<?php get_template_part( \'template-parts/component-page/component\', $variable ); ?>