admin管理员组

文章数量:1389659

I'm trying to add custom rewrite rule for my custom post type. What I want is that www.domain/erlebnisse show all posts (that works). Also I want that www.domain/erlebnisse/category shows all post with category match. And also I want that www.domain/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' );

本文标签: url rewritingCustom rewrite url category