• Resolved sawi

    (@sawi)


    Is there anyway to exclude specific category and sub-categories that fall in it from AMP?

    Or let me put it this way, anyway to include only the articles in x, y and z categories in AMP while leaving the rest to serve non-amp pages?

    Been trying to avoid third party plugins that are way too cluttery for primarily a content-driven site.

    Hoping the authors of the plugin can guide me a bit. Pretty please?

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author Weston Ruter

    (@westonruter)

    Yes. Assuming you have some “amplified” category in which only posts in it should be served as AMP, you can put the following logic in your custom theme or plugin:

    add_filter( 'amp_skip_post', function( $skip, $post ) {
    	if ( ! ( has_category( 'amplified', $post ) || in_category( 'amplified', $post ) ) ) {
    		$skip = true;
    	}
    	return $skip;
    }, 10, 2 );
    • This reply was modified 6 years, 6 months ago by Weston Ruter.
    Thread Starter sawi

    (@sawi)

    Basically, what it is doing is, only converting articles to AMP that are in Amplified category?

    Am I understanding it correctly?

    Furthermore, adding the above code, articles in rest of the categories will be served as non-amp versions e.g. won’t be converted?

    • This reply was modified 6 years, 6 months ago by sawi. Reason: Another question
    Plugin Author Weston Ruter

    (@westonruter)

    It makes articles available in AMP if they are in the Amplified category, yes. Everything else is not made available in AMP.

    In my example I used in_category which I thought handled category descendants as well, but it doesn’t. It is an alias for has_category(). So the above should be just:

    add_filter( 'amp_skip_post', function( $skip, $post ) {
    	if ( ! has_category( 'amplified', $post ) ) {
    		$skip = true;
    	}
    	return $skip;
    }, 10, 2 );

    If you want to handle child categories as well, you could include this: https://wordpress.stackexchange.com/questions/155332/check-if-a-post-is-in-any-child-category-of-a-parent-category

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Anyway to Exclude Specific Categories From AMP’ is closed to new replies.