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

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

Your Answer


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

Browse other questions tagged or ask your own question.