The parent_post_rel_link() WordPress PHP function displays a relational link for the parent item in a hierarchical post type.
Usage
parent_post_rel_link($title_format);
Example:
Input:
parent_post_rel_link('%title');
Output:
<link rel="parent" href="https://example.com/parent-post/" title="Parent Post" />
Parameters
$title_format
(string) Optional: Link title format. Default is ‘%title’.
More information
See WordPress Developer Resources: parent_post_rel_link
This function was introduced in WordPress version 2.8.0.
Examples
Display default parent link
This example displays the default parent link with the ‘%title’ format.
parent_post_rel_link();
Display parent link with custom title format
This example displays the parent link with a custom title format ‘Parent of: %title’.
parent_post_rel_link('Parent of: %title');
Add custom attributes to the parent link
To add custom attributes to the parent link, you can use the ‘parent_post_rel_link’ filter.
add_filter('parent_post_rel_link', 'my_custom_attributes'); function my_custom_attributes($link) { return str_replace('<link', '<link data-custom-attribute="value"', $link); } parent_post_rel_link();
Change the parent link URL
To change the parent link URL, you can use the ‘parent_post_rel_link_href’ filter.
add_filter('parent_post_rel_link_href', 'my_custom_parent_link'); function my_custom_parent_link($link) { return 'https://example.com/custom-parent/'; } parent_post_rel_link();
Remove the parent link from the header
To remove the parent link from the header, you can use the ‘wp_head’ action.
remove_action('wp_head', 'parent_post_rel_link', 10);