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() {

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' );

        $loop = null;

    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 );
  • I will ask you to do an illogical stupid thing: click on "save changes" in settings>> permalinks. Apr 19, 2013 at 11:44
  • I did it but 404 errors persist
    – advalue
    Apr 20, 2013 at 11:36

1 Answer 1


I had the same issue and found a solution. From what I learned so far, there might be two causes to this problem:

  1. WP doesn't recognize your rewrite rules because they are not cached yet. You can check this by dumping get_option( 'rewrite_rules' ); and if your rules did get cached, then they'll be in that option. To ensure your rewrite rules are cached, make sure you call:

    global $wp_rewrite; 
  2. Your server configuration is not properly set to use .htaccess files (for Apache servers). An easy way to check is to type a URL address that doesn't exist after your WordPress host, e.g. http://example.com/location-that-doesnt-exist. If your server is properly configured to use .htaccess files, then you'll see a 404 page styled by your WP theme. If it is NOT properly set, then you will see the server's default 404 page and it will not be styled by your WP theme.

  • 2
    "To ensure your rewrite rules are cached, make sure you call..." – Just not on every page load please. On plugin activation/deactivation only, maybe on save settings.
    – kovshenin
    Apr 2, 2016 at 9:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.