Jetpack

Jetpack Related Posts

Last updated 2 months ago

Set a default image:

PHP
function hlabs_custom_image( $media, $post_id, $args ) {
    if ( $media ) {
        return $media;
    } else {
        $permalink = get_permalink( $post_id );
        $url = apply_filters( 'jetpack_photon_url', 'YOUR_LOGO_IMG_URL' );
      
        return array( array(
            'type'  => 'image',
            'from'  => 'custom_fallback',
            'src'   => esc_url( $url ),
            'href'  => $permalink,
        ) );
    }
}
add_filter( 'jetpack_images_get_images', 'hlabs_custom_image', 10, 3 );

Exclude posts without images:

PHP
function hlabs_find_related_posts_without_image( $hits, $post_id ) {
 
    // Grab any existing list of post IDs to exclude
    $related_posts_ids_to_exclude = get_transient( 'hlabs_posts_without_images' );
    if ( ! $related_posts_ids_to_exclude ) {
        $related_posts_ids_to_exclude = array();
    }
 
    foreach ( $hits as $k => $hit ) {
        if ( class_exists( 'Jetpack_PostImages' ) ) {
            // Check if Jetpack can find an image in a post
            $post_image = Jetpack_PostImages::get_image(
                $hit['id'],
                apply_filters(
                    'jetpack_relatedposts_filter_thumbnail_size',
                    array( 'width' => 350, 'height' => 200 )
                )
            );
            // If Jetpack cannot find an image, add the post ID to $related_posts_ids_to_exclude
            if ( ! is_array( $post_image ) ) {
                if ( ! in_array( $hits[$k], $related_posts_ids_to_exclude ) ) {
                    array_push( $related_posts_ids_to_exclude, $hit['id'] );
                }
            }
        }
    }
    // Save the list of Post IDs to exclude in a transient, for a day
    set_transient( 'hlabs_posts_without_images', $related_posts_ids_to_exclude, DAY_IN_SECONDS );
 
    return $hits;
 
}
add_filter( 'jetpack_relatedposts_filter_hits', 'hlabs_find_related_posts_without_image', 20, 2 );
 
function hlabs_exclude_posts_without_image( $exclude_post_ids, $post_id ) {
 
    /*
     * let's check if another code snippet or plugin already defines posts to exclude.
     * If not, get our transient and exclude these posts.
     * If yes, add our posts to the existing list.
     */
 
    // Get a first list of posts to exclude
    if ( ! $exclude_post_ids ) {
        $exclude_post_ids = array();
    }
 
    // Get the data from our transient
    $related_posts_ids_to_exclude = get_transient( 'hlabs_posts_without_images' );
 
    // Merge any existing list of posts to exclude with the values from our transient
    if ( ! false == $related_posts_ids_to_exclude ) {
        $exclude_post_ids = array_merge( $exclude_post_ids, $related_posts_ids_to_exclude );
    }
 
    return $exclude_post_ids;
 
}
add_filter( 'jetpack_relatedposts_filter_exclude_post_ids', 'hlabs_exclude_posts_without_image', 20, 2 );
PHP
function jetpackme_more_related_posts( $options ) {
    $options['size'] = 6;
    return $options;
}
add_filter( 'jetpack_relatedposts_filter_options', 'jetpackme_more_related_posts' );
PHP
/**
 * Display the post author after the Related Posts context.
 *
 * @param string $context Context displayed below each related post.
 * @param string $post_id Post ID of the post for which we are retrieving Related Posts.
 *
 * @return string $context Context, including information about the post author.
 */
function jetpackme_related_authors( $context, $post_id ) {
    // Get the author ID.
    $post_author = get_post_field( 'post_author', $post_id );
 
    // Get the author's display name.
    $author_display_name = get_the_author_meta( 'display_name', $post_author );
 
    // Add the author name after the existing context.
    if ( isset( $author_display_name ) && ! empty( $author_display_name ) ) {
        return sprintf(
            __( '%1$s<span class="jp-relatedposts-post-author">By %2$s</span>', 'my-plugin-slug' ),
            $context,
            esc_html( $author_display_name )
        );
    }
 
    // Final fallback.
    return $context;
}
add_filter( 'jetpack_relatedposts_filter_post_context', 'jetpackme_related_authors', 10, 2 );
PHP
function jetpackme_remove_rp() {
    if ( class_exists( 'Jetpack_RelatedPosts' ) ) {
        $jprp = Jetpack_RelatedPosts::init();
        $callback = array( $jprp, 'filter_add_target_to_dom' );
 
        remove_filter( 'the_content', $callback, 40 );
    }
}
add_action( 'wp', 'jetpackme_remove_rp', 20 );

Sources:

  • https://jetpack.com/support/related-posts/customize-related-posts/

Tags: #Jetpack

All code snippets are licensed GPLv2 or later unless otherwise stated.