简体   繁体   English

WordPress CPT 在将类别添加到永久链接时导致 404

[英]WordPress CPT causing 404s when categories are added to permalinks

I created a custom post type (CPT) and taxonomies for a gallery that is working fine.我为运行良好的画廊创建了自定义帖子类型 (CPT) 和分类法。

The problem is with the permalinks for non-CPT posts which go to 404s when I add /%category%/ to the permalink structure.问题在于当我将 /%category%/ 添加到永久链接结构时,非 CPT 帖子的永久链接 go 到 404s。

The permalinks work when (1) I remove the custom taxonomy from functions.php, or (2) when I remove /%category%/ from permalinks.当 (1) 我从 functions.php 中删除自定义分类时,或者 (2) 当我从永久链接中删除 /%category%/ 时,永久链接起作用。 Therefore I know the problem is with the CPT or/and taxonomy code in the functions.php but I can not see the problem.因此我知道问题在于函数中的 CPT 或/和分类代码。php 但我看不到问题所在。 Your help is greatly appreciated.非常感谢您的帮助。

/* Custom Post Type - Gallery */
add_action( 'init', 'add_gallery_post_type' );
function add_gallery_post_type() {
    register_post_type(
        'zm_gallery',
        array(
            'labels'             => array(
                'name'          => __( 'The Gallery' ),
                'singular_name' => __( 'The Gallery' ),
                'add_new_item'  => __( 'Add New Photograph' ),
                'all_items'     => __( 'All Images' ),
            ),
            'public'             => true,
            'has_archive'        => true,
            'rewrite'            => array( 'slug' => 'gallery-item' ),
            'supports'           => array( 'title' ),
            'menu_position'      => 4,
            'publicly_queryable' => true,
            'show_ui'            => true,
            'show_in_menu'       => true,
            'query_var'          => true,
            'menu_icon'          => 'dashicons-camera',
            'capability_type'    => 'post',

        )
    );
}


/* Gallery Taxonomies */

function be_register_taxonomies() {

    $taxonomies = array(
        array(
            'slug'        => 'location',
            'single_name' => 'Location',
            'plural_name' => 'Locations',
            'post_type'   => 'zm_gallery',
        ),
        array(
            'slug'        => 'circa',
            'single_name' => 'Circa',
            'plural_name' => 'Circas',
            'post_type'   => 'zm_gallery',
        ),
        array(
            'slug'        => 'era',
            'single_name' => 'Era',
            'plural_name' => 'Era',
            'post_type'   => 'zm_gallery',
        ),
    );

    foreach ( $taxonomies as $taxonomy ) {
        $labels = array(
            'name'              => $taxonomy['plural_name'],
            'singular_name'     => $taxonomy['single_name'],
            'search_items'      => 'Search ' . $taxonomy['plural_name'],
            'all_items'         => 'All ' . $taxonomy['plural_name'],
            'parent_item'       => 'Parent ' . $taxonomy['single_name'],
            'parent_item_colon' => 'Parent ' . $taxonomy['single_name'] . ':',
            'edit_item'         => 'Edit ' . $taxonomy['single_name'],
            'update_item'       => 'Update ' . $taxonomy['single_name'],
            'add_new_item'      => 'Add New ' . $taxonomy['single_name'],
            'new_item_name'     => 'New ' . $taxonomy['single_name'] . ' Name',
            'menu_name'         => $taxonomy['plural_name'],
        );

        $rewrite      = isset( $taxonomy['rewrite'] ) ? $taxonomy['rewrite'] : array( 'slug' => $taxonomy['slug'] );
        $hierarchical = isset( $taxonomy['hierarchical'] ) ? $taxonomy['hierarchical'] : true;

        register_taxonomy(
            $taxonomy['slug'],
            $taxonomy['post_type'],
            array(
                'hierarchical' => $hierarchical,
                'labels'       => $labels,
                'show_ui'      => true,
                'query_var'    => true,
                'rewrite'      => $rewrite,
            )
        );
    }

}

add_action( 'init', 'be_register_taxonomies' );

Okay.好的。 My bad.我的错。 The problem was that the main default category for posts was set to "General".问题是帖子的主要默认类别设置为“常规”。 When I created the CPT I also created a category called "General".当我创建 CPT 时,我还创建了一个名为“General”的类别。 WordPress was getting confused. WordPress 感到困惑。 I renamed the main post category to "News" and now everything works fine.我将主要帖子类别重命名为“新闻”,现在一切正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM