Move Jetpack Related Posts Into a Shortcode
Removes Jetpack's Related Posts from the bottom of posts and lets you place them anywhere with a [wpide_related] shortcode or in your theme.
Opens WPIDE on
Code
add_action( 'wp', function () {
if ( ! class_exists( 'Jetpack_RelatedPosts' ) || ! method_exists( 'Jetpack_RelatedPosts', 'init' ) ) {
return;
}
$jprp = Jetpack_RelatedPosts::init();
remove_filter( 'the_content', array( $jprp, 'filter_add_target_to_dom' ), 40 );
}, 20 );
add_shortcode( 'wpide_related', function () {
if ( ! class_exists( 'Jetpack_RelatedPosts' ) || ! is_singular() ) {
return '';
}
return do_shortcode( '[jetpack-related-posts]' );
} );
Notes
Jetpack also offers its own Related Posts block; this is for classic themes and page builders.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.