# Descriptive Labels for Previous and Next Post Links

> Adds aria-labels like "Previous post: Title" to the previous and next post links in classic themes, so screen readers announce where each link leads.

- Type: PHP
- Runs: Frontend only
- Category: [Accessibility](https://wpide.com/library/category/accessibility/)
- Tags: links, navigation, aria
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/a11y-adjacent-post-link-labels/

## Code

```php
$wpide_adjacent_label = function ( $output, $format, $link, $post, $adjacent ) {
    if ( ! $output || ! $post || ! class_exists( 'WP_HTML_Tag_Processor' ) ) {
        return $output;
    }

    $tags = new WP_HTML_Tag_Processor( $output );

    if ( $tags->next_tag( 'a' ) && ! $tags->get_attribute( 'aria-label' ) ) {
        $prefix = 'previous' === $adjacent ? 'Previous post: ' : 'Next post: ';
        $tags->set_attribute( 'aria-label', $prefix . wp_strip_all_tags( get_the_title( $post ) ) );
    }

    return $tags->get_updated_html();
};

add_filter( 'previous_post_link', $wpide_adjacent_label, 10, 5 );
add_filter( 'next_post_link', $wpide_adjacent_label, 10, 5 );
```

## Notes

Block themes using the Post Navigation Link block already include the title in the link text.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Descriptive Labels for Previous and Next Post Links" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
