Php 在WordPress中加载模板而不回显
我试图编写一个自定义函数,将产品集加载到单独的div中,以在选项卡中使用。每个选项卡都有类别的名称,每个选项卡内容都包含产品 我试图使用woocommerce模板在循环中显示产品,但它立即发出回声,这破坏了设计。我需要将结果接收到output param中,并在最后回显它 在循环类别(foreach为$prod_cat)之后,我的代码如下所示:Php 在WordPress中加载模板而不回显,php,wordpress,include,woocommerce,require-once,Php,Wordpress,Include,Woocommerce,Require Once,我试图编写一个自定义函数,将产品集加载到单独的div中,以在选项卡中使用。每个选项卡都有类别的名称,每个选项卡内容都包含产品 我试图使用woocommerce模板在循环中显示产品,但它立即发出回声,这破坏了设计。我需要将结果接收到output param中,并在最后回显它 在循环类别(foreach为$prod_cat)之后,我的代码如下所示: $args = array( 'post_type' => 'product', 'tax_query' => array(
$args = array(
'post_type' => 'product',
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'tag_ID',
'terms' => $prod_cat['id']
)
),
'order' => 'ASC'
);
$my_query = null;
$my_query = new WP_Query($args);
$counter = 0;
if ($my_query->post_count) {
$output .= '<ul class="products">';
if ($my_query->have_posts()) {
while ($my_query->have_posts()) : $my_query->the_post();
setup_postdata($post);
$output .= woocommerce_get_template_part('content', 'product');
endwhile;
}
$output .= '</ul>';
}
$args=array(
“post_类型”=>“产品”,
“tax_query”=>数组(
排列(
“分类法”=>“产品分类”,
'field'=>'tag_ID',
“术语”=>$prod_cat['id']
)
),
“订单”=>“ASC”
);
$my_query=null;
$my\u query=新的WP\u查询($args);
$counter=0;
如果($my\u query->post\u count){
$output.='';
如果($my\u query->have\u posts()){
while($my_query->have_posts()):$my_query->the_post();
设置_postdata($post);
$output.=woocommerce\u get\u template\u part('content','product');
结束时;
}
$output.='
';
}
因此,它实际上工作正常,可以很好地加载产品,但由于“woocommerce\u get\u template\u part”调用“load\u template”,它会响应文件内容。我希望它返回到$output而不产生任何回音。可能吗
谢谢如果我正确理解了这个问题,您应该可以使用PHP的。类似这样(未经测试):
如果我正确理解了这个问题,您应该能够使用PHP的。类似这样(未经测试):
查看woocommerce\u get\u template()中是否定义了操作。e、 g.执行操作('woocommerce\u get\u template\u part',参数…)。如果是,请通过操作更改对函数的直接调用。谢谢您的回复。自从@Hobo提供的解决方案起作用以来,我还没有尝试过,但我真的很感谢您的帮助。看看woocommerce\u get\u template()是否定义了一个操作。e、 g.执行操作('woocommerce\u get\u template\u part',参数…)。如果是,请通过操作更改对函数的直接调用。谢谢您的回复。自从@Hobo提供的解决方案起作用以来,我还没有尝试过,但我真的非常感谢您的帮助。
while ($my_query->have_posts()) : $my_query->the_post();
setup_postdata($post);
ob_start(); // Start buffering
woocommerce_get_template_part('content', 'product');
$output .= ob_get_clean(); // Get the content of the buffer, and end buffering
endwhile;