Modifying app::title by leaving off the prefix in the title

Inside the App controller, I see the function:

if (is_archive()) {
return get_the_archive_title();
}

I want to change it so the "Category: " prefix doesn’t appear in my title. Therefore, I thought by changing it according to the WP docs, it would leave the prefix off, but it doesn’t according to my example:

if (is_archive()) {
return get_the_archive_title('', false);
}

What am I doing wrong?

I’m not sure what WP docs you were looking at, but get_the_archive_title() doesn’t take any arguments: https://developer.wordpress.org/reference/functions/get_the_archive_title/

This comment on the get_the_archive_title hook documentation offers a possible solution:

function my_theme_archive_title( $title ) {
    if ( is_category() ) {
        $title = single_cat_title( '', false );
    } elseif ( is_tag() ) {
        $title = single_tag_title( '', false );
    } elseif ( is_author() ) {
        $title = '<span class="vcard">' . get_the_author() . '</span>';
    } elseif ( is_post_type_archive() ) {
        $title = post_type_archive_title( '', false );
    } elseif ( is_tax() ) {
        $title = single_term_title( '', false );
    }
  
    return $title;
}
 
add_filter( 'get_the_archive_title', 'my_theme_archive_title' );

@alwaysblank, your answer is the solution I had been using with other themes. But, I thought since something very similar was already specified in Sage’s App controller, then that’s where it should be modified. Thanks for clearing that up.

For things like title I usually want to try and modify them as “close to the source” as possible since generally what I’m trying to do is establish a new site-authoritative title template. Since the Composer is just calling an internal WordPress function, it’s best to filter the behavior of that function, so if I then call get_the_archive_title() in another, non-Composer context I’ll get a title in the template I wish to be the authoritative one, not something else.

Coincidentally I’ve had to do this on a project, and came across an accessibility friendly solution for this if it’s of interest to any future readers.

Instead of completely removing the archive prefix you could visually hide it via the WP class screen-reader-text which would allow screen readers to dictate the archive type with the following code in app/filters.php

/**
 * Hide archive title prefix
 *
 * Instead of fully removing the archive title prefix such as Topics: Topic Name
 * We wrap it in the WordPress required class of .screen-reader-text so that
 * the prefix can still be useful for the impaired yet not affect UX/UI
 */
add_filter( 'get_the_archive_title', function( $title ) {
        // Skip if the site isn't LTR, this is visual, not functional.
	// Should try to work out an elegant solution that works for both directions.
	if ( is_rtl() ) {
		return $title;
	}
	// Split the title into parts so we can wrap them with spans.
	$title_parts = explode( ': ', $title, 2 );
	// Glue it back together again.
	if ( ! empty( $title_parts[1] ) ) {
		$title = wp_kses(
			$title_parts[1],
			array(
				'span' => array(
					'class' => array(),
				),
			)
		);
		$title = '<span class="screen-reader-text">' . esc_html( $title_parts[0] ) . ': </span>' . $title;
	}
	return $title;
});
1 Like

@alwaysblank Thanks for further explanation.
@craigpearson That’s very helpful, offering better context to the user on assistive technology.