wordpress.org – How To Get the Correct Character length of the Category Archives Page Title

Question

I am trying to get the character length of a category archive page. I know i can use

$page_title_length = strlen(get_the_archive_title() ); 

However, for some reason, this gives a higher number than expected. For example, if I go to the archive page of post category ‘uncategorized’, the result i get for the string “Category: Uncategorized” is ’36’, while it should be ’24’! I tried everything, but to no avail.

Here is my whole code:

/**
 * Get Page title letters length
 */

function prefix_get_page_title_length() {
    $page_title_length="";
    if (is_archive() ){
        $page_title_length = strlen( get_the_archive_title() );
    }
    
    else {
        // Everything else
        $page_title_length = strlen( get_the_title() );
    }
    var_dump(get_the_archive_title() );
    return $page_title_length;
}

The code works on single pages (it gets the correct number of letters but it returns more letters for some reason for the category archives. Please help.

0
Badan 4 months 2021-03-22T11:41:31-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse