1

I'm trying to add custom rewrite rule for my custom post type. What I want is that www.domain.com/erlebnisse show all posts (that works). Also I want that www.domain.com/erlebnisse/category shows all post with category match. And also I want that www.domain.com/erlebnisse/post-name shows the post.

What works is:

  • Show all
  • Show post with category

What is not working:

  • Show post (single view)

If I remove my rewrite url single view works. Do i need to add a second rule for single post?

What I got so far:

//Custom Post Type Taxonomies for erlebnisse 
function create_erlebnis_taxonomy() {  
    register_taxonomy(  
        'type',  //The name of the taxonomy. Name should be in slug form (must not contain capital letters or spaces). 
        'erlebnis',        //post type name
        array(  
            'hierarchical' => true,
            'show_in_menu' => true,
            'label' => 'Kategorien - Erlebnisse',  //Display name
            'query_var' => true,
            'has_archive' => true,
            'rewrite' => array('slug' => 'erlebnisse')
        )  
    );  
}  
add_action( 'init', 'create_erlebnis_taxonomy');

function mg_4_custom_post_type() {

    $labels = array(
        'name' => 'Erlebnis',
        'singular_name' => 'Erlebnis',
        'menu_name' => 'Erlebnis',
        'parent_item_colon' => '',
        'all_items' => 'Alle Einträge',
        'view_item' => 'Eintrag ansehen',
        'add_new_item' => 'Neuer Eintrag',
        'add_new' => 'Hinzufügen',
        'edit_item' => 'Eintrag bearbeiten',
        'update_item' => 'Update Eintrag',
        'search_items' => '',
        'not_found' => '',
        'not_found_in_trash' => '',
    );

    $rewrite = array(
        'slug' => 'erlebnisse',
        'with_front' => true,
        'pages' => true,
        'feeds' => true,
    );


    $args = array(
        'labels' => $labels,
        'supports' => array( 'title', 'editor', 'thumbnail' ),
        'taxonomies' => array( 'type', 'post_tag' ),
        'hierarchical' => false,
        'public' => true,
        'show_ui' => true,
        'show_in_menu' => true,
        'show_in_nav_menus' => true,
        'show_in_admin_bar' => true,
        'menu_position' => 5,
        'can_export' => false,
        'has_archive' => true,
        'exclude_from_search' => false,
        'publicly_queryable' => true,
        'rewrite' => $rewrite,
        'capability_type' => 'page',
    );

    register_post_type( 'erlebnis', $args );
}
// Hook into the 'init' action
add_action( 'init', 'mg_4_custom_post_type', 0 );


function taxonomy_erlebnisse_rewrites(){
    add_rewrite_rule( 
        '^erlebnisse/([^/]+)/?$',
        'index.php?post_type=erlebnisse&type=$matches[1]',
        'top'
    );
}
add_action( 'init', 'taxonomy_erlebnisse_rewrites' );

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.