0

When I use single_cat_title() get the name of the current category in archive page, it works perfectly but it prints the category title before the text

<h2>
    <?php 
        if (is_category()){
            echo 'Category: ' . single_cat_title();
        }
    ?>
</h2>

It says:

UncategorizedCategory:

How do I fix it? Thank you.

4

Set the display argument of the function to false.

<h2>
    <?php 
        if (is_category()){
            echo 'Category: ' . single_cat_title( '', false);
        }
    ?>
</h2>

Or, to use the function in its more "helpful" form you could do:

single_cat_title('Category: ');

If the display argument is true, WordPress automatically echoes the content on to the page usually within a wrapper html element and this messes with your echo.

| improve this answer | |

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.