Change category display name function

The question:

Is there a smarter way to write this function? Maybe with wildcards?

add_filter( 'get_the_archive_title', function ( $title ) {
    if ( is_category( 'alphabetical' ) ) {
        $title = 'All Characters';
    }
    elseif ( is_category( 'a-alphabetical' ) ) {
        $title = 'Characters Starting with A';
    }
    elseif ( is_category( 'b-alphabetical' ) ) {
        $title = 'Characters Starting with B';
    }
    elseif ( is_category( 'c-alphabetical' ) ) {
        $title = 'Characters Starting with C';
    }
    elseif ( is_category( 'd-alphabetical' ) ) {
        $title = 'Characters Starting with D';
    }
    elseif ( is_category( 'e-alphabetical' ) ) {
        $title = 'Characters Starting with E';
    }
    elseif ( is_category( 'f-alphabetical' ) ) {
        $title = 'Characters Starting with F';
    }
    elseif ( is_category( 'g-alphabetical' ) ) {
        $title = 'Characters Starting with G';
    }
    elseif ( is_category( 'h-alphabetical' ) ) {
        $title = 'Characters Starting with H';
    }
    elseif ( is_category( 'i-alphabetical' ) ) {
        $title = 'Characters Starting with I';
    }
    elseif ( is_category( 'j-alphabetical' ) ) {
        $title = 'Characters Starting with J';
    }
    elseif ( is_category( 'k-alphabetical' ) ) {
        $title = 'Characters Starting with K';
    }
    elseif ( is_category( 'l-alphabetical' ) ) {
        $title = 'Characters Starting with L';
    }
    elseif ( is_category( 'm-alphabetical' ) ) {
        $title = 'Characters Starting with M';
    }
    elseif ( is_category( 'n-alphabetical' ) ) {
        $title = 'Characters Starting with N';
    }
    elseif ( is_category( 'o-alphabetical' ) ) {
        $title = 'Characters Starting with O';
    }
    elseif ( is_category( 'p-alphabetical' ) ) {
        $title = 'Characters Starting with P';
    }
    elseif ( is_category( 'q-alphabetical' ) ) {
        $title = 'Characters Starting with Q';
    }
    elseif ( is_category( 'r-alphabetical' ) ) {
        $title = 'Characters Starting with R';
    }
    elseif ( is_category( 's-alphabetical' ) ) {
        $title = 'Characters Starting with S';
    }
    elseif ( is_category( 't-alphabetical' ) ) {
        $title = 'Characters Starting with T';
    }
    elseif ( is_category( 'u-alphabetical' ) ) {
        $title = 'Characters Starting with U';
    }
    elseif ( is_category( 'v-alphabetical' ) ) {
        $title = 'Characters Starting with V';
    }
    elseif ( is_category( 'w-alphabetical' ) ) {
        $title = 'Characters Starting with W';
    }
    elseif ( is_category( 'x-alphabetical' ) ) {
        $title = 'Characters Starting with X';
    }
    elseif ( is_category( 'y-alphabetical' ) ) {
        $title = 'Characters Starting with Y';
    }
    elseif ( is_category( 'z-alphabetical' ) ) {
        $title = 'Characters Starting with Z';
    }
    return $title;
});

The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

Method 1

So I wonder why don’t you just set the category title via the admin page, e.g. for the one having the slug alphabetical, you could set All Characters as a static title for the category?

That way, you wouldn’t need to hook on get_the_archive_title..

But anyway, here is one way of how can you reduce those (26) elseif into just one: (using regular expression)

add_filter( 'get_the_archive_title', function ( $title ) {
    if ( is_category( 'alphabetical' ) ) {
        $title = 'All Characters';
    // If the slug starts with <letter>-alphabetical, we set the title to
    // "Characters Starting with <the LETTER in uppercase>".
    } elseif ( is_category() && preg_match( '/^([a-z])-alphabetical$/', get_queried_object()->slug, $m ) ) {
        $title = 'Characters Starting with ' . strtoupper( $m[1] );
    }

    return $title;
} );

So in the above code, I used preg_match(), and also get_queried_object() which returns the term object/data of the current category on a category archive page (e.g. one at example.com/category/alphabetical/).


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Comment