1

I have projects category in my theme ( custom post type ).

my link goes like this

http://shaowtriger.com/project/naim/

my required link is

http://shaowtriger.com/brands/naim/

I also have a page as

http://shaowtriger.com/brands

I am confused how to get this.

Any suggestions on rewrite rules

  • Do not ask for plugin recommendations. That is off topic – Pieter Goosen Sep 16 '14 at 8:58
4

You can use rewrite arg in register_post_type. You can have custom post type name different then rewrite slug. For example here is the sample code with custom post type project and it shows pages with http://shaowtriger.com/brands/naim/

Notice rewrite in $args

add_action( 'init', 'codex_brand_init' );
function codex_brand_init() {
  $labels = array(
    'name'               => _x( 'Brands', 'post type general name', 'your-plugin-textdomain' ),
    'singular_name'      => _x( 'Brand', 'post type singular name', 'your-plugin-textdomain' ),
    'menu_name'          => _x( 'Brands', 'admin menu', 'your-plugin-textdomain' ),
    'name_admin_bar'     => _x( 'Brand', 'add new on admin bar', 'your-plugin-textdomain' ),
    'add_new'            => _x( 'Add New', 'brand', 'your-plugin-textdomain' ),
    'add_new_item'       => __( 'Add New Brand', 'your-plugin-textdomain' ),
    'new_item'           => __( 'New Brand', 'your-plugin-textdomain' ),
    'edit_item'          => __( 'Edit Brand', 'your-plugin-textdomain' ),
    'view_item'          => __( 'View Brand', 'your-plugin-textdomain' ),
    'all_items'          => __( 'All Brands', 'your-plugin-textdomain' ),
    'search_items'       => __( 'Search Brands', 'your-plugin-textdomain' ),
    'parent_item_colon'  => __( 'Parent Brands:', 'your-plugin-textdomain' ),
    'not_found'          => __( 'No brands found.', 'your-plugin-textdomain' ),
    'not_found_in_trash' => __( 'No brands found in Trash.', 'your-plugin-textdomain' )
  );

  $args = array(
    'labels'             => $labels,
    'public'             => true,
    'publicly_queryable' => true,
    'show_ui'            => true,
    'show_in_menu'       => true,
    'query_var'          => true,
    'rewrite'            => array( 'slug' => 'brands' ),
    'capability_type'    => 'post',
    'has_archive'        => true,
    'hierarchical'       => false,
    'menu_position'      => null,
    'supports'           => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments' )
  );

  register_post_type( 'project', $args );
  flush_rewrite_rules();

}

Although having only base page http://shaowtriger.com/brands/ should not interfere with CPT slug and functionality but having subpages to page brands will not work because then WordPress will look for CPT item instead of subpage.

| improve this answer | |
  • i have this already in my functions.php with Projects in place of Brands ..i have changed that slug but its not working – user1145009 Sep 16 '14 at 8:12
  • You will also need to flush_rewrite_rules(); and then visit permalink setting page again. – Robert hue Sep 16 '14 at 8:25
  • didnt get you ..where shud i use flush_rewrite_rules(); – user1145009 Sep 16 '14 at 9:02
  • You can use it when you register CPT. I updated my code above and added flush_rewrite_rules();. You can ad this in your function similarly. – Robert hue Sep 16 '14 at 9:17
  • 3
    Never keep flush_rewrite_rules(); It is a really expensive function to use. Use it once and delete it, otherwise hook it to an action that is run once, like when a theme of a plugin is activated – Pieter Goosen Sep 16 '14 at 9:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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