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
😃+

How to create Tiny Planets (photo spheres) in Android 5.0+ (Lollipop)

When Android 4.2 was released, one of my favorite features was the new Camera app and its Photo Sphere option. It allowed you to take 360-degree panorama pictures, and you could then use the photo editor to create a “tiny planet” from this panorama. I actually published a few here on my blog:

When Android 5.0 (Lollipop) was released, it appeared that the option was gone from the editor. You could still create Photo spheres, but I couldn’t find a way to create the Tiny Planets. Lucky for me, I finally found the option back!

If, like me, you can’t locate the option anymore and would love to create your own little worlds again, without having to use a third-party app, follow the steps below.

How to create a tiny planet in Android 5.0+

A gold mine for your functions.php file

If you have played with WordPress themes in the past, you know that there are many tutorials sites all around the web gathering and proposing small functions to insert into your theme to make it better/harder/faster/stronger…

Here is the new kid on the block: WPfunction.me

Careful though: it is sometimes better to create a small plugin than to paste yet another function in your theme!

WordPress Functions.php snippets
WPFunction.Me lets you quickly build all the functionality you need for your next WordPress project!

A Multisite local environment with subdomains

If you use WordPress multisite locally, you know how painful it can be to add a new entry to your Hosts file every time you start working on a new project. That’s why I chose to work with subdirectories instead of subdomains when I work locally.

Luckily, Konstantin came up with a simple code snippet to add as a plugin o your local instance, that will print a list of all subdomains for that instance. You only have to copy that list and paste it in your Hosts file.

Simple, quick, and a real time saver: you should bookmark this post!

WordPress Multisite with Wildcard Subdomains – Konstantin Kovshenin.

Discover Custom user taxonomies with Justin Tadlock

When WordPress 2.8 came out, we all discover the wonders of taxonomies, and how we could add extra information to our posts and custom post types really easily.

However, it turns out taxonomies have existed for a long time and are not just available for posts, but for all object types. I for one had no idea about this!

Luckily, Justin Tadlock is here! In this tutorial, he covers the basics and examples of what you can do with custom user taxonomies in WordPress; a must-read, as with all tutorials from Justin.

Read the tutorial here: Custom user taxonomies in WordPress.

An introduction to Post Options API

Theme.fm provides us with a great introduction to the Post Options API, that will make you want to drop custom fields altogether.
Flexible, easier to implement, without any needs for custom hardcoded meta boxes, the post options API is a good way to add new options and features on your posts.

Check the tutorial and the source code on Github for more details:

Post Options API: An Alternative to Custom Fields – Theme.fm.

Discover the WordPress settings API

Theme options pages have become increasingly popular in WordPress themes in the past year. There are numerous tutorials that will teach you how to create that page, but not that many of them that explain you how to do it the right way, using the WordPress settings API.

Chip Bennett, member of the theme review team, has put together a great complete team on the best practices and the proper way to create such page, from backend to frontend.

If you want to create WordPress theme options pages, this is a must-read:

Incorporating the Settings API in WordPress Themes » cb.blog.

How to: use a shortcode to display a message to RSS readers only

In his latest article, Konstantin demonstrates how to create a shortcode that is going to allow you to add a specific message to your feed readers only.

That’s a simple and easy way to customize your posts a bit more, and ask your RSS readers to come visit from time to time! :)

Snippet: A “Feed Only” Shortcode for WordPress.

WP Troubleshooting : solve your WordPress problems thanks to video tutorials

If you browse the WordPres.org forums from time to time, you wil quickly realize that some questions keep coming back, and the solutions proposed are often similar. “Check your plugins”, “Do you have access to FTP?”, …

WPtroubleshooting proposes clear and easy answers to all these questions, through video tutorials.

Continue reading

How to: exclude Categories from your RSS Feed

WPvibe proposes a quick way to exclude categories from your RSS feed, and thus display them only on your site, and not for your RSS subscribers.

Careful though, if you use Feedburner, this modification will apply only to your WordPress feed (http://mywordpresssite.com/feed/)

How to Exclude Categories from your RSS Feed.