Using WordPress ‘next_posts_link_attributes’ PHP filter

The next_posts_link_attributes WordPress PHP filter allows you to modify the anchor tag attributes for the next posts page link.

Usage

add_filter('next_posts_link_attributes', 'your_function_name');

function your_function_name($attributes) {
    // your custom code here
    return $attributes;
}

Parameters

  • $attributes (string): The current attributes for the anchor tag.

More information

See WordPress Developer Resources: next_posts_link_attributes

Examples

This example adds a custom CSS class called ‘custom-next-link’ to the next posts link.

add_filter('next_posts_link_attributes', 'add_custom_class_next_posts_link');

function add_custom_class_next_posts_link($attributes) {
    $attributes .= 'class="custom-next-link"';
    return $attributes;
}

This example adds a title attribute with the value ‘Next Posts’ to the next posts link.

add_filter('next_posts_link_attributes', 'add_title_next_posts_link');

function add_title_next_posts_link($attributes) {
    $attributes .= 'title="Next Posts"';
    return $attributes;
}

This example adds a custom data attribute called ‘data-custom’ with the value ‘example’ to the next posts link.

add_filter('next_posts_link_attributes', 'add_data_attribute_next_posts_link');

function add_data_attribute_next_posts_link($attributes) {
    $attributes .= 'data-custom="example"';
    return $attributes;
}

This example adds an id attribute with the value ‘next-posts-link’ to the next posts link.

add_filter('next_posts_link_attributes', 'add_id_attribute_next_posts_link');

function add_id_attribute_next_posts_link($attributes) {
    $attributes .= 'id="next-posts-link"';
    return $attributes;
}

This example adds a CSS class, an id, and a title attribute to the next posts link.

add_filter('next_posts_link_attributes', 'add_multiple_attributes_next_posts_link');

function add_multiple_attributes_next_posts_link($attributes) {
    $attributes .= 'class="custom-next-link" id="next-posts-link" title="Next Posts"';
    return $attributes;
}