• Hello.
    I used custom post types for new posts, and add to them as separate categories, it looks like this –

    add_action('init', 'rooms_register');
    function rooms_register() {
        $labels = array(
            .........
    	);
    
        $args = array(
            'labels'                => $labels,
    		'public'                => true,
    		'publicly_queryable'    => true,
    		'show_ui'               => true,
    		'query_var'             => true,
            'hierarchical'          => false,
    		'menu_icon'             => get_stylesheet_directory_uri() . '/images/icons/rooms-admin.png',
    		'rewrite'               => array(
                'slug' => 'rooms',
                'with_front' => true
            ),
    		'capability_type'       => 'post',
            'taxonomies'            => array('postname',     'rooms_categories'),
    //        'taxonomies'            => array('post_name', 'category'),
    		'menu_position'         => null,
    		'supports'              => array('title', 'editor')
    	  );
    
    	register_post_type( 'rooms' , $args );
    }
    register_taxonomy('rooms_categories',
        array('rooms'),
        array(
            'hierarchical'      => true,
            'label'             => 'Категории',
            'singular_label'    => 'category',
            'rewrite'           => array(
                'slug' => 'rooms',
                'with_front' => false
            ),
        ));
    .......

    After that url to be obtained in this category –
    test.com/rooms/category-name
    And for the post –
    test.com/rooms/post-name
    How to add the category name into the url of post ? I need something like this –
    test.com/rooms/category-name/post-name

    Please help solve the problem

Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    As long as the post slug is at the end of the URL, you could put nearly anything in front and the correct post will still be returned. A significant exception being if the parser should recognize any of the other terms as a query variable, it will try to include them in the query.

    One would think this should not be a problem, but in fact the resulting query ends up being impossible to fulfill so you end up with a 404 error.

    One thing you could do to be able to use such an URL is to hook into ‘pre_get_posts’, check for both ‘name’ (slug) and ‘rooms’ query vars. If both are found, set the ‘rooms’ var to an empty string as the ‘name’ var is all that’s needed to return the correct post.

    One could also use the Rewrite API to accomplish the same thing, but I’ve had trouble creating the right rule that works with all other possible permalinks.

Viewing 1 replies (of 1 total)
  • The topic ‘Custom post_types and taxonomy slugs’ is closed to new replies.