• Vox

    (@voxclamantis)


    In a previous topic I lamented that a renamed category base did not reflect the change in category archive page headings. @alchymyth replied with a perfect solution….though a solution that, twenty months later, I cannot successfully modify.

    Michael’s first if block was fine, but I’d like to customize further by assigning a unique label to each category archive.

    This code will not work. I have been able to accomplish almost the same end by pattern matching against $title, but going forward it would be better if matched against category. I’ve tried various other work-arounds like iterating the category array into a string. I just can’t seem to figure this out. PHP is not my forte; I’m a Perl guy (yes, I know PHP morphed out of Perl, but I find PHP inordinately more obtuse than Perl).

    function use_category_base_in_archive_title( $title ) 
       {if ( is_category() && get_option('category_base') ) 
             $title = str_replace( 'Category:', ucfirst( get_option('category_base') ) . ':', $title );
        elseif ( is_category() && preg_match("/English/i", get_the_category()) ) 
             $title = str_replace( 'Category:', 'Language:', $title );
        elseif ( is_category() && preg_match("/Spanish|Español/i", get_the_category()) ) 
             $title = str_replace( 'Category:', 'Idioma:', $title );
        elseif ( is_category() && preg_match("/French|Français/i", get_the_category()) ) 
             $title = str_replace( 'Category:', 'Langue:', $title );
        elseif ( is_category() && preg_match("/Latine?/i", get_the_category()) ) 
             $title = str_replace( 'Category:', 'Lingua:', $title );
        return $title;
       }
    add_filter( 'get_the_archive_title', 'use_category_base_in_archive_title' );
    • This topic was modified 5 years, 1 month ago by Vox.
    • This topic was modified 5 years, 1 month ago by Vox. Reason: added note about successfully matching against $title

    The page I need help with: [log in to see the link]

Viewing 3 replies - 1 through 3 (of 3 total)
  • if are you trying to check the category title for those strings, and because the code is executed only in category archive pages, you could try to work with https://developer.wordpress.org/reference/functions/single_cat_title/ instead of https://developer.wordpress.org/reference/functions/get_the_category/ (which is more aimed at single posts)

    Thread Starter Vox

    (@voxclamantis)

    Hmm, I can’t seem to implement that and achieve the result that my inarticulate inner brain envisions. But branching from your suggestion, I found

    is_category( array( 9, 'blue-cheese', 'Stinky Cheeses' ) ) 
    Returns true when the category of posts being displayed is either term_ID 9, or slug "blue-cheese", or name "Stinky Cheeses".

    on codex.wordpress.org and I’m wondering if that wouldn’t work. Any reason *not* to use “is_category( array())”?

    Thread Starter Vox

    (@voxclamantis)

    And as an amendment to that, what are the material differences between “is_category()” and “in_category()”? (Sorry, i know this to be an elementary query; it’s just that my brain is so preoccupied by other matters that my own internal processing power is diminished).

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Category Base Rename Is Inconsistent (PART 2)’ is closed to new replies.