Support » Plugin: AMP » amp_post_template_file doesn’t work

  • I’m developing in localhost an AMP version for a live website.
    Only one thing is not working well. The custom template for the homepage is not called.
    I did these steps:

    [x] Add add_theme_support( 'amp', array( 'template_dir' => 'amp-templates', ) ); to my functions.php;
    [x] Check all boxes in AMP Settings;
    [x] Go to Pages, EDIT my Homepage and ENABLE AMP;
    [x] Add the code below in my functions.php

    function xyz_filter_home_and_blog_amp_post_template( $template, $template_type, $post ) {
    	if ( 'page' === $template_type && 'page' === get_option( 'show_on_front' ) ) {
    		if ( (int) get_option( 'page_on_front' ) === $post->ID ) {
    			$template = dirname( __FILE__ ) . '/amp-templates/front-page.php';
    		} elseif ( (int) get_option( 'page_for_posts' ) === $post->ID ) {
    			$template = dirname( __FILE__ ) . '/amp-templates/blog.php';
    	return $template;
    add_filter( 'amp_post_template_file', 'xyz_filter_home_and_blog_amp_post_template', 10, 3 );

    But when I access http://localhost/mysite/?amp (my homepage), the main file front-page.php inside the template root is still called.

    Curiously, the AMP is working on this page, because the files header.php and footer.php inside the root template directory is calling AMP code.

    All the other templates/custom templates are working fine.
    In addition to creating the file theme-root/amp-templates/front-page.php I created the file theme-root/amp-templates/index.php and still not working.

    I tested with these 2 files and WITHOT the filter amp_post_template_file and still no working.

    Another curious thing is: I put this ECHO in the function:

    function xyz_filter_home_and_blog_amp_post_template( $template, $template_type, $post ) {
    	echo "CALLED!";exit;
    	if ( 'page' === $template_type && 'page' === get_option( 'show_on_front' ) ) {
    add_filter( 'amp_post_template_file', 'xyz_filter_home_and_blog_amp_post_template', 10, 3 );

    If this filter was triggered, the page have to break and prints only the word: CALLED
    But, after insert this echo and reload the homepage, the page loads regular. In other words, the filter is not triggered.

    Someone knows what I’m doing wrong?

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


    The amp_post_template_file filter is not used when you have AMP theme support. When AMP theme support is present then the standard template hierarchy is used instead. So you should just name the template files according to the WordPress template hierarchy in the amp-templates directory to have them loaded properly. For example front-page.php for the homepage and home.php for the blog (if I recall correctly).

    Hi @westonruter, thanks the answer. I have removed the filter amp_post_template_file, as you suggested. But I used this filter because the file front-page.php inside my directory amp-templates was not called.

    I have the code in theme-root/functions.php:

    add_theme_support( 'amp', array(
    	'template_dir' => 'amp-templates'
    ) );

    I have this directory: theme-root/amp-templates and inside this directory, I have the file front-page.php. When I access http://localhost/mysite/?amp (my homepage), the template file theme-root/front-page.php is called is called in place of theme-root/amp-templates/front-page.php. All other templates is called correctly. Only homepage doesn’t.

    I have a page which is my HomePage and the AMP is Enabled.
    I’m using WP 4.9.7 and AMP Plugin version 0.7.2.

    I think I’m forgetting something…

    Hi @westonruter, do you have some tip to me? The custom amp template to home page is not working and I don’t know what I have to do.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘amp_post_template_file doesn’t work’ is closed to new replies.