Custom Post Type issue: amp doesn’t work

I’m having trouble (again) with a Custom Post Type created on my WordPress blog.
It is mainly working: I can create a custom post (“articles”) and set a city (taxonomy) to it and it gives me the url I’m wishing to read this page:

https://www.url.com/blog/city/title_of_the_article/

However, it seems that it’s not responding well to some functionnalities or plug-ins. Among them, AMP for WP – Accelerated Mobile Pages.

My AMP is working well everywhere (pages, post, another CPT) except on this CPT “articles” recently created.

In the AMP plug-in settings, I made sure my “articles” is activated in the Custom Post Type support so it should be fine.

What I want:
to have my amp url for this CPT like:
https://www.url.com/blog/city/title_of_the_article/amp/

So even if everything seems to be activated, I have a 404 error when I add /amp/ at the end these pages.

What is strange is that it is working when I try to type https://www.url.com/blog/city/title_of_article/?amp with ?amp at the end, even though it should be /amp/ and that it works with /amp/ for the rest of the posts or pages.

I tried with two other similar plug-in and same issue. I also tried to deactivate all my plug-ins, only leaving AMP activated, but the issue is still there. So I guess it comes from the setting up of this CPT. Here is my code associated with that custom post type and the taxonomy:

register_post_type( ‘article’,
array( ‘labels’ => array(
‘name’ => ‘Articles’,
‘singular_name’ => ‘Article’,
‘all_items’ => ‘All article’,
‘add_new’ => ‘Add article’,
‘add_new_item’ => ‘Add article’,
‘edit’ => ‘Edit article’,
‘edit_item’ => ‘Edit article’,
‘new_item’ => ‘New article’,
‘view_item’ => ‘View article’,
‘search_items’ => ‘Search article’,
‘not_found’ => ‘Nothing found in the Database.’,
‘not_found_in_trash’ => ‘Nothing found in Trash’,
‘parent_item_colon’ => ”
),

‘description’ => ‘This is the example custom post type’,
‘public’ => true,
‘publicly_queryable’ => true,
‘exclude_from_search’ => false,
‘show_ui’ => true,
‘query_var’ => true,
‘menu_position’ => 10,
‘menu_icon’ => ‘dashicons-welcome-write-blog’,
‘rewrite’ => array( ‘slug’ => ‘adresses’, ‘with_front’ => true ),
‘has_archive’ => ‘articles’,
‘capability_type’ => ‘post’,
‘hierarchical’ => false,
‘taxonomies’ => array( ‘category’ ),

‘supports’ => array( ‘title’, ‘editor’, ‘author’, ‘thumbnail’ )));

and

function resource_type() {
$labels = array(
‘name’ => _x( ‘city’, ‘Taxonomy General Name’, ‘snt’ ),
‘singular_name’ => _x( ‘city’, ‘Taxonomy Singular Name’, ‘snt’ ),
‘menu_name’ => __( ‘Add city’, ‘snt’ ),
‘all_items’ => __( ‘All city’, ‘snt’ ),
‘parent_item’ => __( ‘Parent city’, ‘snt’ ),
‘parent_item_colon’ => __( ‘Parent city:’, ‘snt’ ),
‘new_item_name’ => __( ‘New city’, ‘snt’ ),
‘add_new_item’ => __( ‘Add New city’, ‘snt’ ),
‘edit_item’ => __( ‘Edit city’, ‘snt’ ),
‘update_item’ => __( ‘Update city’, ‘snt’ ),
‘view_item’ => __( ‘View city’, ‘snt’ ),
‘separate_items_with_commas’ => __( ‘Separate items with commas’, ‘snt’ ),
‘add_or_remove_items’ => __( ‘Add or remove city’, ‘snt’ ),
‘choose_from_most_used’ => __( ‘Choose from the most used’, ‘snt’ ),
‘popular_items’ => __( ‘Popular city’, ‘snt’ ),
‘search_items’ => __( ‘Search city’, ‘snt’ ),
‘not_found’ => __( ‘Not Found’, ‘snt’ ),
‘no_terms’ => __( ‘No article’, ‘snt’ ),
‘items_list’ => __( ‘Items list’, ‘snt’ ),
‘items_list_navigation’ => __( ‘Items list navigation’, ‘snt’ ),
);
$args = array(
‘labels’ => $labels,
‘hierarchical’ => true,
‘public’ => true,
‘show_ui’ => true,
‘show_admin_column’ => true,
‘show_in_nav_menus’ => true,
‘show_tagcloud’ => true,
‘rewrite’ => array(‘slug’ => ‘city’)
);
register_taxonomy( ‘city’, array( ‘article’ ), $args );}
add_action( ‘init’, ‘resource_type’, 0 );

and

<?php

function resources_cpt_generating_rule($wp_rewrite) {
$rules = array();
$terms = get_terms( array(
‘taxonomy’ => ‘city’,
‘hide_empty’ => false,
) );

$post_type = ‘article’;
foreach ($terms as $term) {
$rules[ $term->slug . ‘/([^/]*)$’] = ‘index.php?post_type=’ .
$post_type. ‘&resources_post_type=$matches[1]&name=$matches[1]’;

}
$wp_rewrite->rules = $rules + $wp_rewrite->rules;
}
add_filter(‘generate_rewrite_rules’, ‘resources_cpt_generating_rule’);

function change_link( $permalink, $post ) {

if( $post->post_type == ‘article’ ) {
$resource_terms = get_the_terms( $post, ‘city’ );
$term_slug = ”;
if( ! empty( $resource_terms ) ) {
foreach ( $resource_terms as $term ) {
if( $term->slug == ‘featured’ ) {
continue;
}
$term_slug = $term->slug;
break;
}
}
if(ICL_LANGUAGE_CODE==”en”){
$permalink = get_home_url() . ‘/’ . $term_slug . ‘/’ . $post->post_name . ‘/’ ;
}else{
$permalink = get_home_url() . $term_slug . ‘/’ . $post->post_name . ‘/’ ;
}
}
return $permalink;
}
add_filter(‘post_type_link’,”change_link”,10,2);

What I want:
https://www.url.com/blog/city/title_of_the_article/amp/ to be working instead of https://www.url.com/blog/city/title_of_the_article/amp?

Thanks for your kind help, I’m kind of lost here… 🙂

Read more here:: Custom Post Type issue: amp doesn’t work

Leave a Reply

Your email address will not be published. Required fields are marked *