• Hi All:

    Any help is greatly appreciated. I am trying to get my permalink structure to reflect both parent and child terms in my custom post type…

    Custom post type is “books”. I have taxonomy called “genre” which has two-levels of terms so a parent/child combo or category/subcategory…

    When viewing a CPT archive page for the child term I get a permalink that looks like this:

    /posttype/taxonomy/term(child)

    What I want is a permalink that look like this:

    /posttype/taxonomy/term(parent)/term(child)

    Any help is greatly appreciated…

    I am registering the CPT with this code:

    function books_taxonomy() {
    	register_taxonomy(
            'book_genre',
            'books',
            array(
                'hierarchical' 			=> true,
                'label' 				=> 'Book Genre',
                'query_var' 			=> true,
                'has_archive'           => true,
    			'capabilities' 			=> array(
                	'edit_terms' 			=> true
    			),
    			'rewrite' 				=> array(
                    'slug' 				=> 'genre',
                    'with_front' 		=> true
        )));
    
    	wp_insert_term('Non-Fiction', 'book_genre');
    	$parent_term_fiction = term_exists('Non-Fiction','book_genre');  // find parent term
    	$parent_term_fiction_id = $parent_term_fiction['term_id']; // get numeric term id
    		wp_insert_term( 'Biography','book_genre', array('slug' => 'biography','parent'=> $parent_term_fiction_id));
    		wp_insert_term( 'Culture','book_genre', array('slug' => 'culture','parent'=> $parent_term_fiction_id));
    		wp_insert_term( 'Food & Wine','book_genre', array('slug' => 'food-wine','parent'=> $parent_term_fiction_id));
    		wp_insert_term( 'Health & Fitness','book_genre', array('slug' => 'health-fitness','parent'=> $parent_term_fiction_id));
    		wp_insert_term( 'History','book_genre', array('slug' => 'history','parent'=> $parent_term_fiction_id));
    		wp_insert_term( 'Religion','book_genre', array('slug' => 'religion','parent'=> $parent_term_fiction_id));
    		wp_insert_term( 'Self Help','book_genre', array('slug' => 'self-help','parent'=> $parent_term_fiction_id));
    		wp_insert_term( 'Travel','book_genre', array('slug' => 'travel','parent'=> $parent_term_fiction_id));
    
    	wp_insert_term('Fiction', 'book_genre');
    	$parent_term_non_fiction = term_exists('Fiction','book_genre');  // find parent term
    	$parent_term_non_fiction_id = $parent_term_non_fiction['term_id']; // get numeric term id
    		wp_insert_term( 'Adventure','book_genre', array('slug' => 'adventure','parent'=> $parent_term_non_fiction_id));
    		wp_insert_term( 'Comedy','book_genre', array('slug' => 'comedy','parent'=> $parent_term_non_fiction_id));
    		wp_insert_term( 'Drama','book_genre', array('slug' => 'drama','parent'=> $parent_term_non_fiction_id));
    		wp_insert_term( 'Mystery & Crime','book_genre', array('slug' => 'mystery-crime','parent'=> $parent_term_non_fiction_id));
    		wp_insert_term( 'Fantasy','book_genre', array('slug' => 'fantasy','parent'=> $parent_term_non_fiction_id));
    		wp_insert_term( 'Poetry','book_genre', array('slug' => 'poetry','parent'=> $parent_term_non_fiction_id));
    		wp_insert_term( 'Romance','book_genre', array('slug' => 'romance','parent'=> $parent_term_non_fiction_id));
    		wp_insert_term( 'Science Fiction','book_genre', array('slug' => 'science-fiction','parent'=> $parent_term_non_fiction_id));
    
    }
    add_action('init', 'books_taxonomy');
    
    function books_post_type() {
    
    	$labels = array(
    		'name'                => _x( 'Books', 'Post Type General Name', 'text_domain' ),
    		'singular_name'       => _x( 'Book', 'Post Type Singular Name', 'text_domain' ),
    		'menu_name'           => __( 'Books', 'text_domain' ),
    		'parent_item_colon'   => __( 'Parent Item:', 'text_domain' ),
    		'all_items'           => __( 'All Books', 'text_domain' ),
    		'view_item'           => __( 'View Books', 'text_domain' ),
    		'add_new_item'        => __( 'Add New Book', 'text_domain' ),
    		'add_new'             => __( 'Add New', 'text_domain' ),
    		'edit_item'           => __( 'Edit Book', 'text_domain' ),
    		'update_item'         => __( 'Update Book', 'text_domain' ),
    		'search_items'        => __( 'Search Books', 'text_domain' ),
    		'not_found'           => __( 'Not Found', 'text_domain' ),
    		'not_found_in_trash'  => __( 'Not Found In Trash', 'text_domain' ),
    	);
    	$rewrite = array(
    		'slug'                => 'books',
    		'with_front'          => true,
    		'pages'               => true,
    		'feeds'               => true,
    	);
    	$args = array(
    		'label'               => __('books', 'text_domain'),
    		'description'         => __('Submitted books for Home Town Reads', 'text_domain' ),
    		'labels'              => $labels,
    		'supports'            => array('title', 'editor', 'excerpt', 'author', 'thumbnail', 'revisions', 'custom-fields', 'page-attributes'),
    		'taxonomies'          => array('continent', 'subject' ),
    		'hierarchical'        => false,
    		'public'              => true,
    		'show_ui'             => true,
    		'show_in_menu'        => true,
    		'show_in_nav_menus'   => true,
    		'show_in_admin_bar'   => true,
    		'menu_position'       => 10,
    		'menu_icon'           => 'dashicons-camera',
    		'can_export'          => true,
    		'has_archive'         => true,
    		'exclude_from_search' => true,
    		'publicly_queryable'  => true,
    		'rewrite'             => $rewrite,
    		'capability_type'     => 'post',
    	);
    	register_post_type('books', $args);
    }
    add_action('init', 'books_post_type', 0);
  • The topic ‘Custom Post Type Permalinks For Parent & Child Terms’ is closed to new replies.