-1

On my category pages, I would like to get the name of the current category, compare it with the values of an array and display it (as a page header, but without a link on it), if the name is identical to one of the strings in the array (meaning: on certain category pages I want to display the category name as a title, on others not). All that above/outside the loop.

I tried this (inside category.php):

<?php
    $visible_titles = array("string1", "string2", "string3");
    if(in_array(the_category(), $visible_titles)) {
        echo "<h1>";
        the_category();
        echo "</h1>";
    }
?>

But from that I get a wrong result: - The category name is displayed as a link even if it's not in the array - The content of the if-clause is never echoed

I also tried to use single_cat_title();, but that (just like the_category) also just echoes the category name as a link – I can't use it as a string to compare it to the values in my array, and I can't display it without it being a link.

What template tag or function can I use to get the desired result?

1
  • may I know the reason for the downvote?
    – Johannes
    Commented Nov 12, 2016 at 0:50

1 Answer 1

1

read https://developer.wordpress.org/reference/functions/single_cat_title/

set the $display parameter to false;

i.e. use: if(in_array( single_cat_title( '', false ), $visible_titles)) {

1
  • Thank you very much! Actually I had read the page you linked to and tried that, but I must have made some typo or other mistake. Thanks again!
    – Johannes
    Commented Nov 12, 2016 at 0:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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