I'm running into some issues while working with a custom post type (CPT). The problem is that the permalinks of the CPT are fine for a single post but not for the archive/overview page.
The website that I'm working should have an archive page with all kind of clothes (CPT) categorized by brand.
The slug of the CPT should be website.com/cloths/brand-name/clothing-name. That's all working fine. Also the taxomomy overview pages like website.com/cloths/brand-name are fine. However the overview/archive page is broken and returns a 404 instead of website.com/cloths/
I really don't know why this is happening especially when the taxonomy and single post permalinks are all fine. I've already changed the permalink settings but without any result. If I create a custom page template with the code that I'm using on the archive template the permalink is fine for the verview/archive page but the pagination is broken, the 2nd page returns a 404.
This is the code that I'm using to create the CPT and desired permalink structure:
function my_custom_post_product() {
$labels = array(
'name' => _x( 'Collectie', 'post type general name' ),
'singular_name' => _x( 'Kleding stuk', 'post type singular name' ),
'add_new' => _x( 'Nieuw kleding stuk', 'reis' ),
'add_new_item' => __( 'Nieuw kleding stuk toevoegen' ),
'edit_item' => __( 'Kleding stuk wijzigen' ),
'new_item' => __( 'Nieuw item' ),
'all_items' => __( 'Alle kleding stukken' ),
'view_item' => __( 'Bekijk item' ),
'search_items' => __( 'Doorzoek collectie' ),
'not_found' => __( 'Geen kleding stuk gevonden ' ),
'not_found_in_trash' => __( 'Geen kleding stuk gevonden in de prullenbak' ),
'parent_item_colon' => '',
'menu_name' => 'Collectie'
);
$args = array(
'labels' => $labels,
'description' => 'Nieuw kleding stuk toevoegen',
'public' => true,
'menu_position' => 5,
'supports' => array( 'title', 'editor', 'thumbnail', 'excerpt', 'comments' ),
'has_archive' => true,
'hierarchical' => true,
'rewrite' => array('slug' => 'kleding/%merk%','with_front' => false),
'query_var' => true,
);
register_post_type( 'kleding', $args );
}
add_action( 'init', 'my_custom_post_product' );
/* Cloths custom taxonomy */
function my_taxonomies_product() {
$labels = array(
'name' => _x( 'Merken', 'taxonomy general name' ),
'singular_name' => _x( 'Merk', 'taxonomy singular name' ),
'search_items' => __( 'Search Product Categories' ),
'all_items' => __( 'Alle merken' ),
'parent_item' => __( 'Parent Product Category' ),
'parent_item_colon' => __( 'Parent Product Category:' ),
'edit_item' => __( 'Edit Product Category' ),
'update_item' => __( 'Update Product Category' ),
'add_new_item' => __( 'Nieuw merk toevoegen' ),
'new_item_name' => __( 'New Product Category' ),
'menu_name' => __( 'Merk' ),
);
$args = array(
'labels' => $labels,
'hierarchical' => true,
'public' => true,
'query_var' => 'merk',
'rewrite' => array('slug' => 'kleding' ),
'_builtin' => false,
);
register_taxonomy( 'merk', 'kleding', $args );
}
add_action( 'init', 'my_taxonomies_product', 0 );
/* Permalink filter Collectie */
add_filter('post_link', 'brand_permalink', 1, 3);
add_filter('post_type_link', 'brand_permalink', 1, 3);
function brand_permalink($permalink, $post_id, $leavename) {
if (strpos($permalink, '%merk%') === FALSE) return $permalink;
// Get post
$post = get_post($post_id);
if (!$post) return $permalink;
// Get taxonomy terms
$terms = wp_get_object_terms($post->ID, 'merk');
if (!is_wp_error($terms) && !empty($terms) && is_object($terms[0]))
$taxonomy_slug = $terms[0]->slug;
else $taxonomy_slug = 'geen-merk';
return str_replace('%merk%', $taxonomy_slug, $permalink);
}
Please note that some parts of the code above are in Dutch. 'merk' = 'brand-name' and 'kleding' = 'cloths'
has_archive
to the desired slug rather than justtrue
?