3

I would like to display the page-title over the category-image. I copied this code

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

from the "template-archive-product.php" to "woocommerce-hook.php" as followed:

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

Now the title doesn't appear. When I am checking the dev tools, the title is like a html-comment.

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

It would be great if someone could help me.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
Remo Girard
  • 33
  • 1
  • 5

2 Answers2

4

The template function woocommerce_page_title() is echoed by default as you can see in its source code… But there is an optional argument to be used to return the data, instead of echoing it. For that you need to turn this argument from (default) true to false.

This is useful when you need to set this data in a variable, just as you are trying to do. So the functional code should be a bit different:

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

Tested and works.

Or alternatively you can use php buffering functions, to get the same thing, this way:

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

Nothing will be outputted and the data will be appended in the $html variable.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
0

Try this code:

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

Instead of:

$html .= '<h1 class="page-title">'<?php woocommerce_page_title(); ?>'</h1>';
Ivnhal
  • 1,099
  • 1
  • 12
  • 20
  • but isn't the lack of echo means that this title would print right out ignoring the = mark? – Sagive Dec 03 '20 at 16:00