I'd like to have comments in a separate page with a dedicated template only for posts in a specific category (slug shops). Example: mysite.com/post-incatshops --> mysite.com/post-incatshops/comment
To do this I create different functions that I put in my functions.php. The problem is that for some posts it returns a 404 error randomly. My permalink structure is: /%postname%. I'm using the plugin WP No Category Base.
Monkeyman Rewrite Analyzer returns for each post in that category this line
^my-post-title/comment p: 16138
Could you please help me and tell me where I'm wrong? Thanks in advance
function setup_permastruct() {
add_rewrite_tag('%opinioni%','([^/]+)');
add_rewrite_tag('%codici-sconto%','([^/]+)');
}
function eg_add_rewrite_rules() {
global $wp_rewrite;
global $wp, $wpdb, $post;
$loop = new WP_Query(array('post_type' => 'post',
'posts_per_page' => -1,
'category_name' => 'shops',
'orderby' => 'date',
'order' => 'DESC')
);
while ( $loop->have_posts() ) : $loop->the_post();
$custom = get_post_custom($post->ID);
add_rewrite_rule( '^'.$post->post_name.'/comment', 'index.php?p='.$post->ID, 'top' );
endwhile;
$loop = null;
wp_reset_query();
$wp_rewrite->flush_rules(false);
}
add_action( 'template_redirect', 'eg_add_rewrite_rules' );
function change_template_include( $t ) {
$url = explode( '/', $_SERVER['REQUEST_URI'] );
$url = array_filter( $url );
$url = array_pop( $url );
if( $url == 'comment' ) {
$t = str_replace( '/single.php', '/template-single-comment.php', $t );
}
return $t;
}
add_filter( 'template_include', 'change_template_include',10, 1 );