Using WordPress ‘adjacent_posts_rel_link_wp_head()’ PHP function

The adjacent_posts_rel_link_wp_head() WordPress PHP function displays relational links for the posts adjacent to the current post on single post pages. It is meant to be attached to actions like ‘wp_head’. Do not call this directly in plugins or theme templates. For a similar function, see also adjacent_posts_rel_link().

Usage

This function is typically used in your theme’s functions.php file. It’s attached to the ‘wp_head’ action, so it automatically prints the relational links in the head section of your site’s HTML. It doesn’t take any parameters and doesn’t return anything.

add_action( 'wp_head', 'adjacent_posts_rel_link_wp_head' );

Parameters

This function does not take any parameters.

More information

See WordPress Developer Resources: adjacent_posts_rel_link_wp_head()

Examples

Basic Usage

In this example, we’re adding the function to the ‘wp_head’ action. This will print the adjacent post links in the head section of your site’s HTML.

// Add the function to the 'wp_head' action
add_action( 'wp_head', 'adjacent_posts_rel_link_wp_head' );

Removing the Function

In this example, we’re removing the function from the ‘wp_head’ action. This will stop the adjacent post links from being printed in the head section.

// Remove the function from the 'wp_head' action
remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head' );

Conditional Usage

In this example, we’re only adding the function to the ‘wp_head’ action if the current page is a single post.

if ( is_single() ) {
    // Add the function to the 'wp_head' action
    add_action( 'wp_head', 'adjacent_posts_rel_link_wp_head' );
}

Function Check

In this example, we’re checking if the function exists before adding it to the ‘wp_head’ action. This is a good practice in case the function gets deprecated in a future version of WordPress.

if ( function_exists( 'adjacent_posts_rel_link_wp_head' ) ) {
    // Add the function to the 'wp_head' action
    add_action( 'wp_head', 'adjacent_posts_rel_link_wp_head' );
}

Using with a Custom Action

In this example, we’re creating a custom action that will run the function. This gives us more control over when and where the function is called.

// Create a custom action that runs the function
do_action( 'my_custom_action' );

// Add the function to our custom action
add_action( 'my_custom_action', 'adjacent_posts_rel_link_wp_head' );