0

I've been messing around with my Woocommerce shop pages and am now just trying to get the category titles to appear at the top of the edited pages again.

I've succeeded so far, using the following code, which does show the right title in the correct spot:

   $titlehere = single_term_title();
    echo '<h1>' . $titlehere . '</h1>';

My issue now is that I need the category title to display in large text as a h1 heading but the HTML formatting isn't applying.

I know there's nothing else clashing with it since other text put in its place shows up on the site with the formatting.

Unfortunately I'm kinda fumbling in the dark here, learning things as I go along so I don't fully understand how everything works.

Is there something I could put in place of "single_term_title()" that will allow me to display the category's title and allow HTML formatting to apply to it?

Thanks in advance!

0

There's nothing about single_term_title(), or any WordPress template tags, that would prevent surrounding HTML from formatting it correctly. All these tags do is output their value. The formatting and appearance of that text is determined by the HTML that is surrounding it, and what your CSS rules are, according to the standard rules of HTML an CSS that apply to all websites. For example, one reason that your text might not appear the way you want is because your theme may lack specific styles for <h1> tags without a class.

EDIT: In double checking the documentation for single_term_title(), I can see that you're using it incorrectly. By default single_term_title() echoes its output, which means that rather than assigning the value to a variable, $titlehere = single_term_title(); is just echoing the title, which means that the result is:

Term title here
<h1></h1>

To properly wrap single_term_title(); in an HTML tag you need to either echo the tags before and after it:

echo '<h1>';
single_term_title();
echo '</h1>';

Or use the set the second $display argument of single_term_title() to false so that the function returns, rather than echoes:

$titlehere = single_term_title( '', false );
echo '<h1>' . $titlehere . '</h1>';
  • Thanks for your answer, Jacob. That's not possible, however, since, when I replace single_term_title() with any other text it shows up on the page with the <h1> formatting applied as intended. – Lily Aug 16 '19 at 5:58
  • I've updated my answer, – Jacob Peattie Aug 16 '19 at 6:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.