Archived topic
Excerpt Read More Arial Label
3 replies · Started by Rachel on February 1, 2023
Hi there,
I use the following snippet for the excerpt read more text. However I am getting warnings as there is no aria-label associated with the read more link. Is there a way to adjust this snippet to add an aria-label for the read more link text?
I assume the standard would be to include the title of the post in the aria-label?
Thanks,
Rachel
add_filter( 'wp_trim_excerpt', 'tu_excerpt_metabox_more' );
function tu_excerpt_metabox_more( $excerpt ) {
$output = $excerpt;
if ( has_excerpt() && ! is_feed() ) {
$output = sprintf( '%1$s %3$s',
$excerpt,
get_permalink(),
__( 'Read more', 'generatepress' )
);
}
return $output;
}
Hi Rachel,
Can you try this code instead?:
add_filter( 'wp_trim_excerpt', 'tu_excerpt_metabox_more' );
function tu_excerpt_metabox_more( $excerpt ) {
$output = $excerpt;
if ( has_excerpt() ) {
$output = sprintf( '%1$s <a href="%2$s" aria-label="%4$s">%3$s</a>',
$excerpt,
get_permalink(),
__( 'Read more', 'generatepress' ),
sprintf(
/* translators: Aria-label describing the read more button */
_x( 'Read more of the post %s', 'more on post title', 'generatepress' ),
the_title_attribute( 'echo=0' )
)
);
}
return $output;
}
Hi Fernando
Great - that has worked, thank you.
I just modified your code slightly to include my exclusion of the read more text from the RSS feed, as was in the original code.
Cheers,
Rachel
add_filter( 'wp_trim_excerpt', 'tu_excerpt_metabox_more' );
function tu_excerpt_metabox_more( $excerpt ) {
$output = $excerpt;
if ( has_excerpt() && ! is_feed() ) {
$output = sprintf( '%1$s %3$s',
$excerpt,
get_permalink(),
__( 'Read more', 'generatepress' ),
sprintf(
/* translators: Aria-label describing the read more button */
_x( 'Read more of the post %s', 'more on post title', 'generatepress' ),
the_title_attribute( 'echo=0' )
)
);
}
return $output;
}
You're welcome, Rachel!