Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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 );
share|improve this question
    
I will ask you to do an illogical stupid thing: click on "save changes" in settings>> permalinks. –  whiteletters in blankpapers Apr 19 '13 at 11:44
    
I did it but 404 errors persist –  advalue Apr 20 '13 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; 
    $wp_rewrite->flush_rules(); 
    
  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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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