• Resolved Irfan_25

    (@irfan_25)


    I’ve added some custom classes in body tag through body_class(); function in my theme. They are available in non amp version but I can’t find them in the amp version.
    Also, WordPress default classes are gone inside of amp.

    Screenshot of the issue:
    https://drive.google.com/file/d/1NEKXfOWKL_ygrdazts9qXSBVwGwhlbk-/view?usp=sharing

    Here is the custom code I’m using in my theme:

    <?php $page_body_class = get_post_meta(get_the_ID(), 'page_body_class', true); ?>
    
    <body <?php body_class($page_body_class); ?>>
    • This topic was modified 4 years, 7 months ago by Irfan_25.
Viewing 1 replies (of 1 total)
  • Plugin Author Weston Ruter

    (@westonruter)

    I assume you’re using Reader mode? If so, body classes are added differently. You can put this code in your theme’s functions.php to add your custom class:

    add_filter(
    	'amp_post_template_data',
    	static function ( $data ) {
    		if ( is_singular() ) {
    			$data['body_class'] .= ' ' . get_post_meta( get_the_ID(), 'page_body_class', true );
    		}
    		return $data;
    	}
    );

    In v2.0 the AMP plugin will allow selecting an entire AMP-compatible theme to be used in Reader mode, for example Twenty Twenty. In that case the normal body_class function and filters will apply. You can use this filter to avoid modifying your templates. When that happens, this would be the preferred code for AMP and non-AMP alike, placed in a custom plugin:

    add_filter( 
    	'body_class', 
    	static function ( $body_class ) {
    		if ( is_singular() ) {
    			$body_class[] = get_post_meta( get_the_ID(), 'page_body_class', true );
    		}
    		return $body_class;
    	} 
    );

    But for the time being, use the amp_post_template_data filter in your theme’s functions.php.

    • This reply was modified 4 years, 7 months ago by Weston Ruter.
Viewing 1 replies (of 1 total)
  • The topic ‘WordPress default and custom classes are gone in’ is closed to new replies.