• Resolved Tyrosh

    (@iskmogul)


    I’m trying to make it so that Jetpack related posts only display on the AMP endpoint pages of my site, and output a certain number of posts. Is there an easy way to make the functions listed below play nice together and code them into my current child theme’s functions?

    I would prefer to keep using the Legacy AMP theme if at all possible, in Reader mode.

    
    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 );
    
    if( function_exists('amp_is_request') && is_amp_endpoint() ) {
    function jetpackme_more_related_posts( $options ) {
        $options['size'] = 6;
        return $options;
    }
    add_filter( 'jetpack_relatedposts_filter_options', 'jetpackme_more_related_posts' );
    }
Viewing 6 replies - 1 through 6 (of 6 total)
  • Hi @iskmogul,

    You can add a function to add content on the footer of your AMP page, for example:

    add_filter( 'amp_post_template_footer', function() {
    	echo do_shortcode( '[jetpack-related-posts]' );
    } );

    I have tried using the function from jetpack but I didn’t have enough views on my test env.

    Hi @iskmogul,

    Just a small correction:

    add_action( 'amp_post_template_footer', function() {
    	echo do_shortcode( '[jetpack-related-posts]' );
    } );
    Thread Starter Tyrosh

    (@iskmogul)

    just to clarify, I want the output to only display on AMP pages, but calling the removerp bit overrides anything else. I tried making an if/else statement with the two functions which relied on amp_is_request, but it wouldn’t work for some reason.

    I also tried adding the shortcode directly to the relevant amp file, but that didn’t work either.

    • This reply was modified 3 years ago by Tyrosh.

    Hi @iskmogul,

    You can use in this case is_amp_endpoint to check the non-AMP version and remove the feature

    <?php
    
    function exclude_jetpack_related_from_non_amp( $options ) {
        if ( function_exists('amp_is_request') && ! amp_is_request() ) {
            $options['enabled'] = false;
        }
    
        return $options;
    }
    
    add_filter( 'jetpack_relatedposts_filter_options', 'exclude_jetpack_related_from_non_amp' );

    I’m not so familiar with jetpack methods, but you can change the way to remove it.

    As we didn’t receive a response I’ll mark this as resolved.

    Feel free to open a new support topic if you require any further assistance.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Jetpack Functions’ is closed to new replies.