WordPress.org

Ready to get started?Download WordPress

Forums

CPT-onomies: Using Custom Post Types as Taxonomies
404 page when accessing CPT-onomy post (2 posts)

  1. noblestreet
    Member
    Posted 3 months ago #

    Hi All,

    I've been trying to get this to work but havent made any success.
    The issue is as follows:
    I maded a CPT in the backend 'showcase' and added posts to the CPT. When i tried to view the post initially i got a 404 page.
    So I added a rewrite rule:

    function add_showcase_rules() {
     global $wp_rewrite;
     add_rewrite_rule(
          "showcase/(.+)/?$",
          "index.php?post_type=showcase&page=$matches[1]",
          "top");
    }
    add_action( 'init', 'add_showcase_rules' );

    and set the permalink to 'http://s2.noblestreet.eu/%postname%/'
    and added a template include function:

    function tax_page_template( $template ) {
      global $post_type;
      if ( $post_type == 'showcase' || $post_type == 'work' ) {
          if ( $custom_template = locate_template( "single-$post_type.php" ) )
             return $custom_template;
       }
       return $template;
    }

    Now when i go to this page:
    http://s2.noblestreet.eu/?showcase=decoder-specs
    I see the proper page, but i dont have proper URL handling

    With proper URL handling, i see ALL the posts related post_type 'showcase' >
    http://s2.noblestreet.eu/showcase/decoder-specs/

    The question:
    How can i get AND proper URL handling AND only the relevant post?

    Thanks in advance : )

    https://wordpress.org/plugins/cpt-onomies/

  2. noblestreet
    Member
    Posted 3 months ago #

    Ok in the meantime i have done some diggin around my self. I ended up going thru the class-wp.php file (WP::parse_request). I came up with the following rewrite rule which works with my CPT.

    function add_cpt_rules() {
      global $wp_rewrite;
      add_rewrite_rule(
        "showcase/([^/]+)(/[0-9]+)?/?$",
        "index.php?showcase=" . $wp_rewrite->preg_index(1) . "&page=" . $wp_rewrite->preg_index(2),
        "top");
    
      add_rewrite_rule(
        "work/([^/]+)(/[0-9]+)?/?$",
        "index.php?work=" . $wp_rewrite->preg_index(1) . "&page=" . $wp_rewrite->preg_index(2),
        "top");
    
      $wp_rewrite->flush_rules();
    }
    add_action( 'init', 'add_cpt_rules' );

    showcase and work are my additional CPTs i removed the extra template include function. As WP now grabs the post_type for the relevant template file.

    Using Debug This plugin helped a lot. (http://websynthesis.com/fixing-wordpress-404-errors/)

    regards,

    Arnaud

Reply

You must log in to post.

About this Plugin

About this Topic