Jetpack: Add Related Posts to your RSS feed

I got an interesting question in the Jetpack support forums today. Levy wanted to display Related Posts in their RSS feed.

Jetpack displays Related Posts at the bottom of single posts by default, but like with other modules, you can customize Related Posts. In this post, we’ll use the the_content filter and the raw Jetpack Related Posts class to build our own unordered list of Related Posts, and add it to the bottom of the post content in RSS feeds.

Quick note before we start:

  • You’ll want to add that code to a functionality plugin like this one.
  • Nothing will happen if you’re not using Jetpack and its Related Posts module.
/**
 * Add Jetpack Related Posts to RSS feed.
 *
 * @see https://wordpress.org/support/topic/2927523
 */
function jeherve_related_posts_feed( $content ) {
	// Return early if we're not in the RSS feed.
	if ( ! is_feed() ) {
		return $content;
	}

	// If Jetpack and Related Posts are active, let's get started.
	if ( class_exists( 'Jetpack_RelatedPosts' ) && method_exists( 'Jetpack_RelatedPosts', 'init_raw' ) ) {
		$related = Jetpack_RelatedPosts::init_raw()
			->set_query_name( 'jetpackme-shortcode' ) // Optional, name can be anything
			->get_for_post_id(
				get_the_ID(),
				array( 'size' => 3 )
			);

		if ( $related ) {
			$related_list = '';

			foreach ( $related as $result ) {
				// Get the related post IDs
				$related_post_id = get_post( $result[ 'id' ] );

				/**
				 * From there you can do just about anything, using the post IDs.
				 *
				 * In this example, we'll build an unordered list.
				 */
				$related_list .= sprintf(
					'<li><a title="%1$s" href="%2$s">%3$s</a></li>',
					esc_attr( get_the_title( $related_post_id ) ),
					get_permalink( $related_post_id ),
					get_the_title( $related_post_id )
				);
			}

			/**
			 * Let's wrap all those related posts in ul tags, and add that list to the end of our post content.
			 *
			 * We will also add a headline, but only if it was set to be displayed in your Jetpack Related Posts settings.
			 */
			$related_options = Jetpack_Options::get_option( 'relatedposts' );
			if ( $related_options['show_headline'] ) {
				$headline = sprintf(
					'<h3 class="jp-relatedposts-headline"><em>%s</em></h3>',
					esc_html__( 'Related', 'jetpack' )
				);
			} else {
				$headline = '';
			}

			return sprintf(
				'%1$s%2$s<ul class="jp-relatedposts">%3$s</ul>',
				$content,
				apply_filters( 'jetpack_relatedposts_filter_headline', $headline ),
				$related_list
			);
		}

		return $content;
	}

	// Last fallback, just in case Jetpack and Related Posts aren't there anymore.
	return $content;
}
add_filter( 'the_content', 'jeherve_related_posts_feed' );
😎
1
😃+

Come see me at WordCamp Europe

Are you at WordCamp Europe this weekend? Come say hi, I am at the Jetpack booth! 🚀 ☺ 

👍
7
😃+

A week in Dublin

Working for Automattic is pretty great. I work from home, and also get to travel from time to time. After Hawaii, India, San Francisco, Spain, and a few other places, we decided to head to Dublin for a week to meet, and work on delivering better support for Jetpack, VaultPress, and Akismet users.

Continue reading

🇮🇪
8
😀
1

Color Posts update for AMP

If you follow the news, you most likely heard about the Accelerated Mobile Pages Project in the past few weeks. This open source initiative, lead by Google and a few big publishing and technology companies, allows you to serve super fast pages to mobile readers, when they come from a Google Search result or from an app using AMP:

How AMP looks like when launching a Google search

Automattic joined the project and released the AMP plugin, so you can enable AMP on your own site with a few clicks.

It’s not super pretty, though. By default, AMP pages sport a standard blue title bar. That’s where Color Posts comes in! 😊  I just updated the plugin, and it will now change the look of the title bar to match the post’s main color.

All you have to do is install the AMP plugin, and update Color Posts!

😀
1

Developers: discover Jetpack’s little secrets

You probably already know the Jetpack plugin. But did you know it included developer features you can use when building sites for your clients?

In this article we’ll discover some of Jetpack’s little secrets, and learn how to use them in our projects.

Discover Jetpack’s secrets

Les secrets de Jetpack — WordCamp Paris

Pour ma première participation à WordCamp Paris, j’ai décidé de parler d’un sujet qui me tient à coeur : Jetpack !

Voici donc ma présentation : en attendant que la vidéo soit mise en ligne, vous pouvez parcourir mes notes ci-dessous, jeter un coup d’oeil à mon diaporama, tout en écoutant mes commentaires dans la vidéo au bas de cet article.

Découvrez les secrets de Jetpack pour gagner du temps dans vos projets

Static Site Generators: are we doomed?

I am writing this in response to this post. Start there and come back; I’ll wait!
Continue reading

Jetpack: add UTM tracking to sharing buttons

After figuring out a nice little challenge in the Jetpack support forums last week, I wanted to share the results with you.

First of all, I need to thank Aquif Shaikh for his question in the original thread. This thread required a bit more digging than usual, and I love a good challenge.

The question was actually quite simple: how to add UTM tracking to the Jetpack sharing buttons. If you’re not familiar with UTM tracking, you’ll want to check this generator. UTM tracking allows you to track specific URLs in Google Analytics.

Let’s get started.

Continue reading

Découvrez la face cachée de Jetpack au WordCamp Paris

WordCamp Paris 2016, c’est l’occasion pour tous les fans de WordPress de se rencontrer, le 5 et 6 février, à Paris. Vous y serez ? Moi aussi, il faut qu’on se voit ! 😊

Continue reading

New theme, new logo, new PHP!

Fresh coat of paint for my blog: new theme, new logo, new version of PHP, and HTTP/2!

Check our the new looks