Using WordPress ‘get_parent_post_rel_link()’ PHP function

The get_parent_post_rel_link() WordPress PHP function retrieves the parent post relational link.

Usage

get_parent_post_rel_link($title);

Input:
$title (string) – Optional. Link title format. Default: ‘%title’.

Output:
Parent post relational link as a string.

Parameters

  • $title (string) – Optional. The format of the link title. Default value is ‘%title’.

More information

See WordPress Developer Resources: get_parent_post_rel_link()

Examples

Basic Usage

Get the parent post relational link with default title format:

// Get the parent post link
$parent_post_link = get_parent_post_rel_link();

// Display the parent post link
echo $parent_post_link;

Custom Title Format

Get the parent post relational link with a custom title format:

// Get the parent post link with custom title format
$parent_post_link = get_parent_post_rel_link('Go to parent post: %title');

// Display the parent post link
echo $parent_post_link;

Using in a Template File

Display the parent post link in a template file:

// Display the parent post link in a template file
echo get_parent_post_rel_link();

Custom HTML Wrapper

Wrap the parent post link in custom HTML:

// Get the parent post link
$parent_post_link = get_parent_post_rel_link();

// Display the parent post link with custom HTML wrapper
echo '<p>Parent Post: ' . $parent_post_link . '</p>';

Conditional Display

Display the parent post link only if it exists:

// Get the parent post link
$parent_post_link = get_parent_post_rel_link();

// Check if the parent post link exists and display it
if (!empty($parent_post_link)) {
    echo '<p>Parent Post: ' . $parent_post_link . '</p>';
} else {
    echo '<p>No parent post available.</p>';
}