I created a custom post type (CPT) and taxonomies for a gallery that is working fine.
The problem is with the permalinks for non-CPT posts which go to 404s when I add /%category%/ to the permalink structure.
The permalinks work when (1) I remove the custom taxonomy from functions.php, or (2) when I remove /%category%/ from permalinks. Therefore I know the problem is with the CPT or/and taxonomy code in the functions.php but I can not see the problem. 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". WordPress was getting confused. I renamed the main post category to "News" and now everything works fine.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.