我在使用自定义帖子类型(CPT)时遇到了一些问题。问题是,CPT的永久链接对于一篇文章来说很好,但对于归档/概述页面则不行。
我正在工作的网站应该有一个按品牌分类的各种衣服(CPT)的归档页面。
CPT的鼻涕虫应该是网站。面料/布料/品牌/服装名称。一切都很好。还有Taxommy概述页面,如网站。面料/布料/品牌都很好。但是,概述/归档页面被破坏,返回404而不是网站。面料/布/
我真的不知道为什么会发生这种情况,尤其是在分类法和单后永久链接都很好的情况下。我已经更改了永久链接设置,但没有任何结果。如果我使用存档模板上使用的代码创建自定义页面模板,则permalink对于概览/存档页面很好,但分页被破坏,第二个页面返回404。
这是我用来创建CPT和所需permalink结构的代码:
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);
}
请注意,上面代码的某些部分是荷兰语。”merk’=“brand-name”和“kleding”=“Cloth”