‘previous_posts_link_attributes’ is a WordPress PHP filter that allows you to modify the anchor tag attributes for the “previous posts” page link.
Usage
add_filter( 'previous_posts_link_attributes', 'custom_previous_posts_link_attributes' );
function custom_previous_posts_link_attributes( $attributes ) {
// Your custom code here
}
Parameters
- $attributes (string)
- The attributes for the anchor tag.
Examples
Adding a CSS class to the previous posts link
add_filter( 'previous_posts_link_attributes', 'add_class_to_previous_posts_link' );
function add_class_to_previous_posts_link( $attributes ) {
return 'class="custom-previous-posts-link"';
}
This code adds the custom-previous-posts-link CSS class to the previous posts link.
Adding a custom title attribute to the previous posts link
add_filter( 'previous_posts_link_attributes', 'add_title_to_previous_posts_link' );
function add_title_to_previous_posts_link( $attributes ) {
return 'title="Go to the previous page"';
}
This code adds a custom title attribute “Go to the previous page” to the previous posts link.
Adding multiple attributes to the previous posts link
add_filter( 'previous_posts_link_attributes', 'add_multiple_attributes_to_previous_posts_link' );
function add_multiple_attributes_to_previous_posts_link( $attributes ) {
return 'class="custom-previous-posts-link" title="Go to the previous page"';
}
This code adds both a CSS class and a custom title attribute to the previous posts link.
Adding an aria-label attribute for accessibility
add_filter( 'previous_posts_link_attributes', 'add_aria_label_to_previous_posts_link' );
function add_aria_label_to_previous_posts_link( $attributes ) {
return 'aria-label="Navigate to the previous posts"';
}
This code adds an aria-label attribute to the previous posts link to improve accessibility.
Combining existing attributes with new attributes
add_filter( 'previous_posts_link_attributes', 'combine_attributes_to_previous_posts_link' );
function combine_attributes_to_previous_posts_link( $attributes ) {
$new_attributes = 'class="custom-previous-posts-link"';
return $attributes . ' ' . $new_attributes;
}
This code combines the existing attributes with new attributes, allowing you to preserve any existing attributes while adding new ones. In this example, a CSS class is added to the previous posts link.