• Resolved Rangga

    (@jurnalkreatif)


    Hi..

    [ask] is there any one could help me how to Replace one of the Related Posts by a custom result on all post with jackpack.

    i found his on jetpack support page :
    Replace one of the Related Posts by a custom result, for a specific post

    function jetpackme_append_related_post( $hits, $post_id ) {
        // $post_id is the post we are currently getting related posts for
            if ( 2194 == $post_id ) {
                // Add 1036 to the front of the hits array
            array_unshift( $hits, array( 'id' => 1036 ) );
                // Remove the last element of the array
                array_pop( $hits );
            }
         return $hits;
    }
    add_filter( 'jetpack_relatedposts_filter_hits', 'jetpackme_append_related_post', 20, 2 );

    but that code is for Replace one of the Related Posts by a custom result, for a specific post.

    the question is how do i Replace one of the Related Posts by a custom result on all post in my blog. please help.. i’m not very good with code..

    https://www.ads-software.com/plugins/jetpack/

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author Jeremy Herve

    (@jeherve)

    Jetpack Mechanic ??

    how do i Replace one of the Related Posts by a custom result on all post in my blog

    You can start from the code above, but remove the conditional that targets post 2194. You’ll want to make sure that post ID is not added as related post to itself, though. Here is how it would look:

    function jetpackme_append_related_post( $hits, $post_id ) {
        // $post_id is the post we are currently getting related posts for
            if ( ! is_single( '1036' ) ) {
                // Add 1036 to the front of the hits array
            array_unshift( $hits, array( 'id' => 1036 ) );
                // Remove the last element of the array
                array_pop( $hits );
            }
         return $hits;
    }
    add_filter( 'jetpack_relatedposts_filter_hits', 'jetpackme_append_related_post', 20, 2 );

    You’ll only need to replace 1036 by your post ID.

    Thread Starter Rangga

    (@jurnalkreatif)

    wow.. its work just the way i wanted.. big thanks for the help Jeremy

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Replace one of the Related Posts by a custom result’ is closed to new replies.