Skip to content

Related Posts

Ben Gillbanks edited this page Aug 21, 2019 · 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 :)

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();
    }
}

Clone this wiki locally