Skip to content

Related Posts

Ben Gillbanks edited this page Oct 4, 2020 · 11 revisions

Toolbelt can automatically append related posts to the end of post content.

The related posts will be selected randomly from posts in the same categories as the post being viewed. A collection of posts are saved in a transient (specific to the current post), and then 2 posts are selected and appended to the end of the post content.

Related posts with images are given priority, since they look nicer :)

Block

The Related Posts module includes a block. This is primarily added to be used in Full Site Editing functionality that's coming in the future.

If the block is used then the default behaviour of adding related posts to the end of the post content will be disabled.

Filters

toolbelt_related_posts_count

Change how many related posts are displayed.

By default 2 posts are displayed.

function my_toolbelt_related_posts_count() {
    return 4;
}
add_filter( 'toolbelt_related_posts_count', 'my_toolbelt_related_posts_count' );

toolbelt_related_posts_thumbnail_size

The thumbnail size to use for the featured images. By default the medium image size is used.

function my_toolbelt_related_posts_thumbnail_size() {
    return 'some-image-size-handle';
}
add_filter( 'toolbelt_related_posts_thumbnail_size', 'my_toolbelt_related_posts_thumbnail_size' );

toolbelt_related_posts

Use this to stop related posts from being added to the end of the_content. You can then echo the toolbelt_related_posts_get function to output related posts wherever you like.

add_filter( 'toolbelt_related_posts', '__return_false' );
// Add this to your theme files and then call my_related_posts() in your theme.
function my_related_posts() {
    if ( function_exists( 'toolbelt_related_posts_get' ) ) {
        echo toolbelt_related_posts_get();
    }
}

toolbelt_related_posts_types

Add a custom post type to the list of post types that can display related posts.

For this to work we need to know the registered post type, and the related taxonomy that should be used for filtering the posts.

function my_related_posts_type( $types ) {
	$types['post_type'] = 'post_taxonomy';
	return $types;
}
add_filter( 'toolbelt_related_post_types', 'my_related_posts_type' );

You can see an example of this in action in this very plugin. I add support for the portfolio post type, but only if the post type has been activated.

Clone this wiki locally