1

What I want to do and what I got is: one custom post types ('cpt') x categories (i.e. 'topics'). Now I want the url path to be like http://domain.com/page-x/page-y/%topics%/cpt. What I got is stated below. Works fine but how can I replace the %topics% with the according category?

Thank you so much in advance.

add_action( 'init', 'create_topics_hierarchical_taxonomy', 0 );

function create_topics_hierarchical_taxonomy() {

  $labels = array(
    'name' => _x( 'topics', 'taxonomy general name' ),
    'singular_name' => _x( 'topics', 'taxonomy singular name' ),
    'search_items' =>  __( 'Search topics' ),
    'all_items' => __( 'All topics' ),
    'parent_item' => __( 'Parent topics' ),
    'parent_item_colon' => __( 'Parent topics' ),
    'edit_item' => __( 'Edit topics' ), 
    'update_item' => __( 'Update topics' ),
    'add_new_item' => __( 'Add New topics' ),
    'new_item_name' => __( 'New topics' ),
    'menu_name' => __( 'topics' ),
  );    

  register_taxonomy('topics',array('cpt'), array(
    'hierarchical' => true,
    'labels' => $labels,
    'show_ui' => true,
    'show_admin_column' => true,
    'query_var' => true,
    'rewrite' => array( 'slug' => 'page-x/page-y' ),
  ));

}



function create_posttype_cpt() {

    register_post_type( 'cpt',
        array(
            'labels' => array(
                'name' => __( 'cpt' ),
                'singular_name' => __( 'cpt' )
            ),
            'public' => true,
            'rewrite' => array('slug' => 'page-x/page-y/%topics%', 'with_front' => false ),
                'has_archive' => 'page-x/page-y',
                'menu_icon' => 'dashicons-id-alt',
        )
    );
}

add_action( 'init', 'create_posttype_cpt' );


function custom_post_type_cpt() {

    $labels = array(
        'name'                => _x( 'cpt', 'Post Type General Name', 'arvada-child-theme' ),
        'singular_name'       => _x( 'cpt', 'Post Type Singular Name', 'avada-child-theme' ),
        'menu_name'           => __( 'cpt', 'avada-child-theme' ),
        'parent_item_colon'   => __( 'Parent cpt', 'avada-child-theme' ),
        'all_items'           => __( 'All cpt', 'avada-child-theme' ),
        'view_item'           => __( 'View cpt', 'avada-child-theme' ),
        'add_new_item'        => __( 'Add New cpt', 'avada-child-theme' ),
        'add_new'             => __( 'Add New', 'avada-child-theme' ),
        'edit_item'           => __( 'Edit cpt', 'avada-child-theme' ),
        'update_item'         => __( 'Update cpt', 'avada-child-theme' ),
        'search_items'        => __( 'Search cpt', 'avada-child-theme' ),
        'not_found'           => __( 'Not Found', 'avada-child-theme' ),
        'not_found_in_trash'  => __( 'Not found in Trash', 'avada-child-theme' ),
    );


    $args = array(
        'label'               => __( 'cpt', 'avada-child-theme' ),
        'description'         => __( 'cpt', 'avada-child-theme' ),
        'labels'              => $labels,

        'supports'            => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'custom-fields', 'revisions', 'page-attributes', 'post-formats' ),



        'hierarchical'        => true,
        'public'              => true,
        'show_ui'             => true,
        'show_in_menu'        => true,
        'show_in_nav_menus'   => true,
        'show_in_admin_bar'   => true,
        'can_export'          => true,
        'exclude_from_search' => false,
        'publicly_queryable'  => true,
        'capability_type'     => 'page',
    );

    register_post_type( 'cpt', $args );

}




add_action( 'init', 'custom_post_type_cpt', 0 );
  • see this question. – Milo Jul 30 '16 at 23:43
  • awesome, now it works - first I forgot to refresh the opened site again so when I tried the was still the old url with the placeholder. I refreshed it and now it works. Perfect, thank you. What If I have an item with more than one category? – new Android Jul 31 '16 at 5:08
  • is there a to show it also in the breadcrumb – new Android Jul 31 '16 at 5:19
  • what if I have a sub-category - this does not show up. How could I implement that? – new Android Jul 31 '16 at 6:46
  • where can I change the template of archive page-y – new Android Jul 31 '16 at 7:43

Your Answer

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

Browse other questions tagged or ask your own question.