6 Ways To Show nRelate Related Posts In Themes

nRelate is one of the best plugins for displaying related posts in WordPress because it doesn’t use any hosting resources. Related posts plugins are notorious for guzzling CPU which can cause major problems if you ‘re on shared servers.

This is NOT the case with this plugin as all the processing uses the resources from nRelates servers. This means you can also use it if you’re hosting on WPengines super fast servers as its not on the banned plugin list.

nRelate related posts plugin for WordPress

This tutorial will focus on the different ways you can change the display position using different methods.

Use The Plugins Layout Settings

Firstly, lets take a look at the layout settings the plugin offers.

layout settings

The plugins layout options only provide one hook in the after post position so lets take a look at how your can change that.

To change the position nRelate displays in your theme you can:

1. Configure the plugins layout settings to display in most areas.
2. Use the nRelate template tag in a custom function with Genesis hook.
3. Use the nRelate shortcode in a custom function with Genesis hook.
4. Use the nRelate widget in a custom widget.
5. Use the nRelate shortcode in a custom widget and add support for shortcodes in widgets.
6. Add the template tag to any themes template file.

nRelate Template Tag

Here’s the PHP template tag:

<?php if (function_exists('nrelate_related')) nrelate_related(); ?>

nRelate shortcode in a text widget

Here’s some of the different shortcodes you can use pretty much anywhere in your theme including text widgets.

[nrelate-related] Will use defaults
[nrelate-related float='right']
[nrelate-related width='50%']
[nrelate-related float='right' width='50%']

Add Support For Shortcodes in Widgets

If you paste a shortcode in a text widget, you will need to add theme support for shortcodes in widgets.

Note: These PHP code snippets use the new Genesis 2.0 HTML 5 hooks which you can use with the 3rd parameter for priority positioning.

Hook In nRelate Template Tag Using Genesis

Here’s the code you can use or modify in your child themes functions.php file:

add_action('genesis_after_entry', 'nRelate_after_single_posts');
function nRelate_after_single_posts() {
if ( is_single() ) {
nrelate_related();

  }

}

Simply change the hook position and use a different conditional tag to suit your own needs.

Hook In nRelate shortcode Using Genesis

add_action('genesis_after_entry', 'nrelate_after_category_posts');
function nrelate_after_category_posts() {
if (in_category('web-design') ) {
    echo do_shortcode('[nrelate-related]');
   }
}

The above code will display your related posts only after posts in a specific category.

You can add or exclude multiple categories using an array using the template tag or shortcode:

add_action('genesis_after_entry', 'nrelate_after_single_posts');
function nrelate_after_single_posts() {
if (in_category( array( 10,20,30 ) ) ) {
    echo do_shortcode('[nrelate-related]');
   }
}

Use the nRelate widget in a custom widget

Create a custom widget with conditional tag and hook in the widget in any theme position.

genesis_register_sidebar( array(
'id' => 'nrelate-widget',
'name' => __( 'nRelate Widget', 'wpsites' ),
'description' =>  __( 'This is the nRelate widget area before all primary sidebars on single posts.', 'wpsites' ),
) );

add_action( 'genesis_before_sidebar_widget_area', 'nrelate_before_sidebar_widget', 9 );
function nrelate_before_sidebar_widget() {
if ( is_single() && is_active_sidebar('nrelate-widget') ) {
echo '<div class="nrelate-widget">';
	dynamic_sidebar( 'nrelate-widget');
	echo '</div><!-- end .nrelate-widget -->';

	}

}

The above example creates a widget before all the primary sidebars on single posts.

Add the template tag to any themes template file

You can simply paste thge nRelate template tag after the loop in any themes single.php file.

Here’s the tag in the Twenty Fourteen default themes file.

single posts template

Note: You should copy this file over to a child theme so it won’t be lost when you update the parent theme.

Other Options

You can use this code with the template tag or shortcode for other related posts plugins.

If you’re not using Genesis, you can replace the hooks with your own themes specific hooks and the code will work. Otherwise you’ll need to use the shortcodes in a text widget, page/post or any of your themes template files.

Well i think this pretty much covers all bases however please fire away in the comments if you need help or have feedback to share.

Join 5000+ Followers

Get The Latest Free & Premium Tutorials Delivered The Second They’re Published.