Halloween Sale 15% off Ends in See the plans
PHP

Customize Jetpack Related Posts

Changes the heading, number of posts and thumbnail visibility of Jetpack's Related Posts, and limits suggestions to posts from the last two years.

Don’t have WPIDE yet? Install it on your site

Code

$wpide_headline = 'You might also enjoy'; // Heading text.
$wpide_count    = 3;                       // Posts to show (max 6).
$wpide_years    = 2;                       // Only suggest posts from the last N years; 0 = no limit.

add_filter( 'jetpack_relatedposts_filter_headline', function () use ( $wpide_headline ) {
    return '<h3 class="jp-relatedposts-headline"><em>' . esc_html( $wpide_headline ) . '</em></h3>';
} );

add_filter( 'jetpack_relatedposts_filter_options', function ( $options ) use ( $wpide_count ) {
    $options['size'] = max( 1, min( 6, (int) $wpide_count ) );

    return $options;
} );

add_filter( 'jetpack_relatedposts_filter_date_range', function ( $range ) use ( $wpide_years ) {
    if ( ! $wpide_years ) {
        return $range;
    }

    return array(
        'from' => strtotime( '-' . (int) $wpide_years . ' years' ),
        'to'   => time(),
    );
} );

Notes

Edit the values at the top. Jetpack can show at most 6 related posts.

How to add this snippet

  1. Install WPIDE on your WordPress site (it is free) and open Code Snippets.
  2. Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
  3. It is added inactive: review the code and settings, then switch it on.

Have a snippet worth sharing?

Publish it to the library from WPIDE or right here. Sign in with any WordPress site that runs WPIDE, free or Pro. Public snippets are reviewed by the WPIDE team before they appear.

Which site should get this snippet?

Enter the address of your WordPress site. WPIDE opens there in a new tab with the snippet ready to review and save.

Remembered in this browser only, so you are asked once.

Report this snippet

Tell the WPIDE team what is wrong: broken code, something harmful, spam or copied work.