dongyan2469 2016-02-23 10:49
浏览 95
已采纳

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 2016-02-23 11:12
    关注

    I solved it by myself.

    I had to change:

    'has_archive'           => true,
    

    to:

    'has_archive'           => 'branchenbuch',
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 本题的答案是不是有问题
  • ¥15 关于#r语言#的问题:(svydesign)为什么在一个大的数据集中抽取了一个小数据集
  • ¥15 C++使用Gunplot
  • ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 蓝桥杯单片机第十三届第一场,整点继电器吸合,5s后断开出现了问题
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 Arcgis相交分析无法绘制一个或多个图形