WordPress - 自定义帖子类型单页上的 404 错误 [英] WordPress - 404 error on custom post type single pages

查看:25
本文介绍了WordPress - 自定义帖子类型单页上的 404 错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用自定义帖子类型,该类型也应具有自定义分类法.当我访问存档页面/kollektion/时,我可以看到该 CPT 的所有项目,当我访问/kollektion/mytaxonomy1/时,我可以看到分配给该分类法的所有项目.

I use a custom post type, which also should have a custom taxonomy. When I visit the archive page /kollektion/ I can see all items of this CPT, when I visit /kollektion/mytaxonomy1/ I can see all items which are assigned to this taxonomy.

当我单击自定义帖子类型的单个项目时,出现 404 错误.我还尝试进入我的 WP 仪表板并再次更改并重新保存永久链接 - 没有成功.

When I click on the single item of the custom post type, I get a 404 error. I also tried to go into my WP dashboard and change and resave the permalinks again - with no success.

有没有人看到我的声明中有错误导致我的自定义帖子类型的单页出现 404 错误?

Does anybody see an error in my declaration which causes the 404 error on single pages of my custom post type?

谢谢.

functions.php:分类

// register two taxonomies to go with the post type
function sm_register_taxonomy() {
    // set up labels
    $labels = array(
        'name'              => 'Kollektionen Categories',
        'singular_name'     => 'Kollektion Category',
        'search_items'      => 'Search Kollektion Categories',
        'all_items'         => 'All Kollektion Categories',
        'edit_item'         => 'Edit Kollektion Category',
        'update_item'       => 'Update Kollektion Category',
        'add_new_item'      => 'Add New Kollektion Category',
        'new_item_name'     => 'New Kollektion Category',
        'menu_name'         => 'Kollektion Categories'
    );
    // register taxonomy
    register_taxonomy( 'kollektionen', 'kollektion', array(
        'hierarchical' => true,
        'labels' => $labels,
        'query_var' => true,
        'show_admin_column' => true,
        'rewrite' => array(
            'slug' => 'kollektion', // This controls the base slug that will display before each term
            'with_front' => false // Don't display the category base before
        ),
    ) );
}
add_action( 'init', 'sm_register_taxonomy' );

functions.php:自定义帖子类型

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

        $labels = array(
            'name'                  => _x( 'Kollektionen', 'Post Type General Name', 'genesis' ),
            'singular_name'         => _x( 'Kollektion', 'Post Type Singular Name', 'genesis' ),
            'menu_name'             => __( 'Kollektion', 'genesis' ),
            'name_admin_bar'        => __( 'Kollektion', 'genesis' ),
            'parent_item_colon'     => __( 'Parent Item:', 'genesis' ),
            'all_items'             => __( 'All Items', 'genesis' ),
            'add_new_item'          => __( 'Add New Item', 'genesis' ),
            'add_new'               => __( 'Add New', 'genesis' ),
            'new_item'              => __( 'New Item', 'genesis' ),
            'edit_item'             => __( 'Edit Item', 'genesis' ),
            'update_item'           => __( 'Update Item', 'genesis' ),
            'view_item'             => __( 'View Item', 'genesis' ),
            'search_items'          => __( 'Search Item', 'genesis' ),
            'not_found'             => __( 'Not found', 'genesis' ),
            'not_found_in_trash'    => __( 'Not found in Trash', 'genesis' ),
            'items_list'            => __( 'Items list', 'genesis' ),
            'items_list_navigation' => __( 'Items list navigation', 'genesis' ),
            'filter_items_list'     => __( 'Filter items list', 'genesis' ),
        );
        $args = array(
            'label'                 => __( 'Kollektion', 'genesis' ),
            'description'           => __( 'Kollektionen', 'genesis' ),
            'labels'                => $labels,
            'supports'              => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', ),
            'taxonomies'            => array( 'post_tag', 'kollektionen' ),
            'hierarchical'          => false,
            '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'           => 'kollektion',
            'exclude_from_search'   => false,
            'publicly_queryable'    => true,
            'capability_type'       => 'page',
        );
        register_post_type( 'kollektion', $args );

    }
    add_action( 'init', 'post_type_kollektion');
}

function namespace_add_custom_types( $query ) {
  if( is_category() || is_tag() && empty( $query->query_vars['suppress_filters'] ) ) {
    $query->set( 'post_type', array(
     'post', 'nav_menu_item', 'kollektion', 'kollektionen'
        ));
      return $query;
    }
}
add_filter( 'pre_get_posts', 'namespace_add_custom_types' );

推荐答案

看起来您对分类和自定义帖子类型有相同的 slug.尝试更改分类(或 CPT)的 slug 并重新保存永久链接 - 这应该会有所帮助.

It looks like you have the same slug for taxonomy and custom post type. Try to change slug of taxonomy (or CPT) and resave the permalinks - this should help.

这篇关于WordPress - 自定义帖子类型单页上的 404 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆