dongyan2469
2016-02-23 10:49 阅读 92
已采纳

WordPress - 自定义帖子类型存档页面 - 404

I use custom post types on my page and managed it that the new taxonomy is included in the URL. Everything works well, but when I visit example.com/branchenbuch/ I get a 404.

example.com/branchenbuch/ -> fails example.com/branchenbuch/customtaxonomy -> works example.com/branchenbuch/customtaxonomy/post -> works

Does anybody know why?

The code goes as follows:

if ( ! function_exists('register_custom_post_types') ) {

    // Register Custom Post Type Kollektion
    function register_custom_post_types() {

        add_rewrite_tag('%kategorie%','(.+)');

        $labels = array(
            'name'                  => _x( 'Branchenbuch', 'Post Type General Name', 'genesis' ),
            'singular_name'         => _x( 'Branchenbuch', 'Post Type Singular Name', 'genesis' ),
            'menu_name'             => __( 'Branchenbuch', 'genesis' ),
            'name_admin_bar'        => __( 'Branchenbuch', 'genesis' ),
            'parent_item_colon'     => __( 'Parent Item:', 'genesis' ),
            'all_items'             => __( 'Alle Einträge', 'genesis' ),
            'add_new_item'          => __( 'Neuen Eintrag hinzufügen', 'genesis' ),
            'add_new'               => __( 'Neu hinzufügen', 'genesis' ),
            'new_item'              => __( 'Neu', 'genesis' ),
            'edit_item'             => __( 'Editieren', 'genesis' ),
            'update_item'           => __( 'Aktualisieren', 'genesis' ),
            'view_item'             => __( 'Ansehen', 'genesis' ),
            'search_items'          => __( 'Suche', 'genesis' ),
            'not_found'             => __( 'Nicht gefunden', 'genesis' ),
            'not_found_in_trash'    => __( 'Nicht im Papierkorb gefunden', 'genesis' ),
            'items_list'            => __( 'Liste', 'genesis' ),
            'items_list_navigation' => __( 'Liste Navigation', 'genesis' ),
            'filter_items_list'     => __( 'Filter', 'genesis' ),
        );
        $args = array(
            'label'                 => __( 'Branchenbuch', 'genesis' ),
            'description'           => __( 'Branchenbuch', 'genesis' ),
            'labels'                => $labels,
            'supports'              => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', ),
            'taxonomies'            => array( 'branchenbuch-kategorie' ),
            'hierarchical'          => true,
            'public'                => true,
            'show_ui'               => true,
            'show_in_menu'          => true,
            'menu_position'         => 5,
            'show_in_admin_bar'     => true,
            'show_in_nav_menus'     => true,
            'can_export'            => true,
            'has_archive'           => true,
            'exclude_from_search'   => false,
            'publicly_queryable'    => true,
            'capability_type'       => 'page',
            'rewrite'       => array('slug' => 'branchenbuch/%kategorie%','with_front' => false),
        );
        register_post_type( 'branchenbuch', $args );

    }//function
    add_action( 'init', 'register_custom_post_types');
}

// register two taxonomies to go with the post type
function custom_register_taxonomy() {
    // set up labels
    $labels = array(
        'name'              => 'Branchenbuch Kategorien',
        'singular_name'     => 'Branchenbuch Kategorie',
        'search_items'      => 'Suche',
        'all_items'         => 'Alle Kategorien',
        'edit_item'         => 'Editieren',
        'update_item'       => 'Aktualisieren',
        'add_new_item'      => 'Neu hinzufügen',
        'new_item_name'     => 'Neu hinzufügen',
        'menu_name'         => 'Kategorien'
    );
    // register taxonomy
    register_taxonomy( 'branchenbuch-kategorie', 'branchenbuch', array(
        'hierarchical' => true,
        'labels' => $labels,
        'query_var' => true,
        'show_admin_column' => true,
        'rewrite' => array(
            'slug' => 'branchenbuch-kategorie', // This controls the base slug that will display before each term
        ),
    ) );
}
add_action( 'init', 'custom_register_taxonomy' );

/* Filter modifies the permaling */

add_filter('post_link', 'custom_category_permalink', 1, 3);
add_filter('post_type_link', 'custom_category_permalink', 1, 3);

function custom_category_permalink($permalink, $post_id, $leavename) {
    if (strpos($permalink, '%kategorie%') === FALSE) return $permalink;
        // Get post
        $post = get_post($post_id);
        if (!$post) return $permalink;

        // Get taxonomy terms
        $terms = wp_get_object_terms($post->ID, 'branchenbuch-kategorie');
        if (!is_wp_error($terms) && !empty($terms) && is_object($terms[0]))
            $taxonomy_slug = $terms[0]->slug;
        else $taxonomy_slug = 'sonstiges';

    return str_replace('%kategorie%', $taxonomy_slug, $permalink);
}
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

2条回答 默认 最新

  • 已采纳
    doubiaokai4998 doubiaokai4998 2016-02-23 11:12

    I solved it by myself.

    I had to change:

    'has_archive'           => true,
    

    to:

    'has_archive'           => 'branchenbuch',
    
    点赞 评论 复制链接分享
  • douli8428 douli8428 2018-06-03 10:33
    1. Navigate to Settings -> permalinks
    2. Change the permalink structure to Default
    3. Save settings
    4. Change to custom structure or post name (or any other structure)
    5. Save Settings

    This will re-write the htaccess file and then the re-write should work.

    I hope this will save your time.

    点赞 评论 复制链接分享

相关推荐