apply_filters ( 'jetpack_relatedposts_filter_post_context', string $this->_to_utf8(, string $post_id )

Filter the context displayed below each Related Post.

Source file: modules/related-posts/jetpack-related-posts.php

View in GitHub

Parameters

$this->_to_utf8(

(string) $this->_generate_related_post_context( $post->ID ) ) Context displayed below each related post.

$post_id

(string) Post ID of the post for which we are retrieving Related Posts.


Changelog

Since: Jetpack 3.0.0


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 the jetpack_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 );

Have a note to contribute?

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s