Filter the context displayed below each Related Post. This context is used when rendering the legacy ‘widget’ version of Related Posts. It is not used when rendering the block-based version. See ‘block_context’ below for that.
Parameters
- $post_id
int Post ID of the post for which we are retrieving Related Posts.
- $this->to_utf8(
string $this->generate_related_post_context( $post->ID ) ) Context displayed below each related post.
Changelog
- Introduced in Jetpack 3.0.0
How to use this hook
Notes
By default, Jetpack adds a category name, a tag name, or “Similar Post” under each one of the Related Posts on your site. You can use thejetpack_relatedposts_filter_post_context filter to remove, or edit this context.
To remove that context, you can just ask the filter to return an empty string, like so:
add_filter( 'jetpack_relatedposts_filter_post_context', '__return_empty_string' );To add more information, you can follow the example below, where we add the author name to the Related Post context:
/**
* Display the post author after the existing Related Posts context.
*
* @param string $context Context displayed below each related post.
* @param string $post_id Post ID of the post for which we are retrieving Related Posts.
*
* @return string $context Context, including information about the post author.
*/
function jeherve_related_authors( $context, $post_id ) {
// Get the author ID.
$post_author = get_post_field( 'post_author', $post_id );
// Get the author's display name.
$author_display_name = get_the_author_meta( 'display_name', $post_author );
// Add the author name after the existing context.
if ( isset( $author_display_name ) && ! empty( $author_display_name ) ) {
return sprintf(
__( '%1$s<span class="jp-relatedposts-post-author">By %2$s</span>', 'my-plugin-slug' ),
$context,
esc_html( $author_display_name )
);
}
// Final fallback.
return $context;
}
add_filter( 'jetpack_relatedposts_filter_post_context', 'jeherve_related_authors', 10, 2 );