ampproject / amp-wp

Enable AMP on your WordPress site, the WordPress way.
https://wordpress.org/plugins/amp/
GNU General Public License v2.0
1.79k stars 382 forks source link

AMP for Custom Post Types not Working #645

Closed webliskatech closed 7 years ago

webliskatech commented 7 years ago

Hello

I tried to follow the exact guide to add support for custom post types for AMP.

Here is my blog: https://www.tuluzz.com

The Setup I am Using:

  1. Theme - Genesis Framework
  2. Toolset Types Plugin for Creating Custom Post Types
  3. My Custom post Types are : Hardwares, Softwares, Gadgets and Laptops
  4. AMP Plugin by Automattic
  5. Accelerated Mobile Pages Plugin by ampforwp.com

So, According to the Solution, I added this code to my Site:

add_action( 'amp_init', 'xyz_amp_add_review_cpt' );
function xyz_amp_add_review_cpt() {
    add_post_type_support( 'hardwares', AMP_QUERY_VAR );
     add_post_type_support( 'softwares', AMP_QUERY_VAR );
   add_post_type_support( 'gadgets', AMP_QUERY_VAR );
   add_post_type_support( 'laptops', AMP_QUERY_VAR );
}

Also, Flushed my Permalinks Structure again as per the documentation.

So, When I checked the https://www.tuluzz.com/hardwares/amp

it should work, but its not working but giving a 404 Error Page.

Please help, what I can do about this to solve it.

amedina commented 7 years ago

The link https://www.tuluzz.com/hardwares/ corresponds to a kind of index page where multiple posts are shown. Could you provide links to actual posts of the custom types?

amedina commented 7 years ago

The check indicated is behaving appropriately since the link is not a post. Closing this for now; please reopen if still an issue and provide more information to try to track it down.

myleshyson commented 5 years ago

@amedina Hey I'm having the same issue. I have a testimonials custom post type, and have the archive option enabled. When I add this code snippet I expect both single testimonial pages and the testimonials archive to be redirected the the ?amp url when I append /amp.

The single post type page works /testimonials/chris/amp

The archive page does not and throws a 404 error /testimonials/amp

add_action('amp_init', function () {
    add_post_type_support('testimonials', amp_get_slug());
});
westonruter commented 5 years ago

@myleshyson The classic mode of the plugin only supports AMP on singular post type templates. If you want AMP on non-singular templates, you'll need to switch to the new Paired or Native modes. In these modes, you're active theme's templates and stylesheets will be served in the AMP responses and all URLs of your site are then able to be served as AMP.

myleshyson commented 5 years ago

@westonruter Hey thanks for getting back. I actually am using the paired option and it definitely keeps giving me a 404 error.

screen shot 2018-12-21 at 15 53 00-fullpage

myleshyson commented 5 years ago

This is the workaround I came up with to get it to work but ideally this would work out of the box. I had to write some custom code to get the front page to work as well.

    add_action('parse_query', function ($query) {
        $slug = amp_get_slug();
        $name = $query->query['name'] ?? null;
        if ($name = $slug && $_SERVER['REQUEST_URI'] === "/$slug") {
            $query->is_home = false;
            $query->is_page = true;
            $query->is_singular = true;
            $query->set('page_id', get_option('page_on_front'));
        }
    });

    add_filter('request', function ($query_vars) {
        $slug = amp_get_slug();
        $name = $query->query['name'] ?? null;
        $postType = $query_vars['post_type'] ?? false;
        if ($name = $slug && $_SERVER['REQUEST_URI'] === "/$slug") {
            $query_vars[amp_get_slug()] = 1;
        }
        if (isset($query_vars['name']) && $query_vars['name'] === amp_get_slug() && $postType === 'testimonials') {
            $query_vars[amp_get_slug()] = 1;
        }
        if (isset($query_vars[amp_get_slug()]) && '' === $query_vars[amp_get_slug()]) {
            $query_vars[amp_get_slug()] = 1;
        }
        return $query_vars;
    });
westonruter commented 5 years ago

@myleshyson You shouldn't need this. Without your custom code, look at the non-AMP version of the page. In the page source, you should see an amphtml link in the head. You should see that it ends in ?amp not /amp/. The /amp/ URLs are only used in classic mode.

myleshyson commented 5 years ago

@westonruter ah ok thanks! That makes sense now. I guess it is still necessary for us then because we like the /amp/ url better than using the ?amp url.

westonruter commented 5 years ago

Why does the URL matter? Normally the full paired URL is not exposed when accessing an AMP page via Google Search, Twitter, etc.

myleshyson commented 5 years ago

I guess it doesn't haha. Purely an OCD vanity thing.