将woocommerce_page_title存储在变量中并将其显示在Woocommerce中

问题描述 投票:1回答:2

我想在类别图像上显示页面标题。我复制了这段代码

<h1 class="page-title"><?php woocommerce_page_title(); ?></h1>

从“template-archive-product.php”到“woocommerce-hook.php”如下:

$html .= '<h1 class="page-title">'<?php woocommerce_page_title(); ?>'</h1>';

现在标题没有出现。当我检查开发工具时,标题就像一个html-comment。

<h1 class="page-title"><!--?php woocommerce_page_title(); ?--></h1>

如果有人可以帮助我会很棒。

php wordpress templates woocommerce page-title
2个回答
2
投票

模板function woocommerce_page_title()默认回显,你可以在its source code中看到......但是有一个可选参数用于返回数据,而不是回显数据。为此,您需要将此参数从(默认)true转换为false

当您需要在变量中设置此数据时,这非常有用,就像您尝试的那样。所以功能代码应该有点不同:

$html .= '<h1 class="page-title">' . woocommerce_page_title( false ); . '</h1>';

经过测试和工作。

或者你可以使用php缓冲函数来获得相同的东西,这样:

ob_start();
?>
<h1 class="page-title"><?php woocommerce_page_title(); ?></h1>
<?php
$html .= ob_get_clean();

什么都不输出,数据将附加在$html变量中。


0
投票

试试这段代码:

$html .= '<h1 class="page-title">' . woocommerce_page_title() .'</h1>';

代替:

$html .= '<h1 class="page-title">'<?php woocommerce_page_title(); ?>'</h1>';
© www.soinside.com 2019 - 2024. All rights reserved.