这是我的第一篇帖子,所以我可能会错过一些信息,抱歉:鞠躬。
我的问题和Paul T几乎一样(Permalinks: custom post type -> custom taxonomy -> post), 即:
我有一个自定义的post类型,名为sioban\\u cpt,它有一个自定义的分类法sioban\\u taxo。
sioban\\U cpt可以more than one sioban\\u taxo。
我希望我的URL以这种方式路由:
www.mysite.com/sioban_cpt/ => archive-sioban-cpt.php
www.mysite.com/sioban_cpt/%sioban_taxo%/ => taxonomy-sioban_taxo.php
**www.mysite.com/sioban_cpt/%postname%/ => single-sioban_cpt.php**
因此:
www.mysite.com/sioban_cpt/ => shows all posts with sioban_cpt post-type (this works)
www.mysite.com/sioban_cpt/foo/ => shows all posts with the foo taxonomy(this works)
www.mysite.com/sioban_cpt/bar => shows the indivual post under the sioban_cpt post-type (this doesn\'t work, it display the error template, although www.mysite.com/sioban_cpt/bar is the shown permalink in the WP admin post edition page)
作为PaulT,我尽量避免使用插件。
我不知道解决方案是否可行,因为我希望分类法和CPT的帖子都是CPT的后代,相反,我不希望CPT的帖子是taxo的子类。
这是我的函数代码。php:
/**
* create new custom taxonomy (has to be called before CPT declaration)
*
* @return void
*/
function si0b_ct_year()
{
/* Property Type */
$labels = array(
\'name\' => _x(\'Years\', \'Taxonomy General Name\', \'siobanone\'),
\'singular_name\' => _x(\'Year\', \'Taxonomy Singular Name\', \'siobanone\'),
\'menu_name\' => __(\'Année de participation\', \'siobanone\'),
\'all_items\' => __(\'Toutes les années\', \'siobanone\'),
\'new_item_name\' => __(\'Nouvelle année\', \'siobanone\'),
\'add_new_item\' => __(\'Ajouter une nouvelle année\', \'siobanone\'),
\'edit_item\' => __(\'Modifier l\\\'année\', \'siobanone\'),
\'update_item\' => __(\'Actualiser l\\année\', \'siobanone\'),
\'view_item\' => __(\'Voir l\\année\', \'siobanone\'),
\'separate_items_with_commas\' => __(\'Separate years with commas\', \'siobanone\'),
\'add_or_remove_items\' => __(\'Add or remove years\', \'siobanone\'),
\'search_items\' => __(\'Search Years\', \'siobanone\'),
\'not_found\' => __(\'Not Found\', \'siobanone\'),
\'no_terms\' => __(\'No years\', \'siobanone\'),
\'items_list\' => __(\'Years list\', \'siobanone\'),
\'items_list_navigation\' => __(\'Years list navigation\', \'siobanone\'),
);
$rewrite = array(
\'slug\' => __(\'aec-year\', \'siobanone\'),
\'with_front\' => false
);
$args = array(
\'labels\' => $labels,
\'public\' => true,
\'show_ui\' => true,
\'show_admin_column\' => true,
\'show_in_nav_menus\' => true,
\'show_in_quick_edit\' => true,
\'rewrite\' => $rewrite,
\'show_in_rest\' => true,
\'has_archive\' => true,
);
register_taxonomy(\'aec-year\', \'artists\', $args);
}
add_action(\'init\', \'si0b_ct_year\', 10);
/*To create new custom post type */
function si0b_cpt_artists()
{
// CPT : artists
$labels = array(
\'name\' => _x(\'Artists\', \'Post Type General Name\', \'siobanone\'),
\'singular_name\' => _x(\'Artist\', \'Post Type Singular Name\', \'siobanone\'),
\'menu_name\' => __(\'Artists\', \'siobanone\'),
\'name_admin_bar\' => __(\'Artists\', \'siobanone\'),
\'parent_item_colon\' => __(\'Parent Item:\', \'siobanone\'),
\'all_items\' => __(\'All artists\', \'siobanone\'),
\'add_new_item\' => __(\'Ajouter nouvel.le artiste\', \'siobanone\'),
\'add_new\' => __(\'Ajouter\', \'siobanone\'),
\'new_item\' => __(\'New Item\', \'siobanone\'),
\'edit_item\' => __(\'Edit Item\', \'siobanone\'),
\'update_item\' => __(\'Update Item\', \'siobanone\'),
\'view_item\' => __(\'View Item\', \'siobanone\'),
\'search_items\' => __(\'Search Item\', \'siobanone\'),
\'not_found\' => __(\'Not found\', \'siobanone\'),
\'not_found_in_trash\' => __(\'Not found in Trash\', \'siobanone\'),
);
$rewrite = array(
\'slug\' => _x(\'artistes/%aec-year%\', \'siobanone\'),
\'with_front\' => false
);
$args = array(
\'label\' => __(\'artists\', \'siobanone\'),
\'description\' => _x(\'Artists\', \'siobanone\'),
\'labels\' => $labels,
\'supports\' => array(\'title\', \'editor\', \'thumbnail\', \'comments\', \'revisions\', \'custom-fields\', \'page-attributes\', \'excerpt\'),
\'taxonomies\' => array(\'aec-year\'),
\'hierarchical\' => false,
\'public\' => true,
\'show_ui\' => true,
\'show_in_menu\' => true,
\'menu_position\' => 5,
\'menu_icon\' => \'dashicons-images-alt\',
\'show_in_admin_bar\' => true,
\'show_in_nav_menus\' => true,
\'can_export\' => true,
\'has_archive\' => \'artistes\',
\'exclude_from_search\' => false,
\'rewrite\' => $rewrite,
);
register_post_type(\'artists\', $args);
}
add_action(\'init\', \'si0b_cpt_artists\', 10);
/**
* add a filter to post_type_link to substitute the taxo aec-year in individual CPT artists permalinks
*
* @param [type] $post_link
* @param [type] $post
* @return void
*/
function si0b_artists_permalinks($post_link, $post)
{
if (is_object($post) && $post->post_type == \'artists\') {
$terms = wp_get_object_terms($post->ID, \'aec-year\');
if ($terms) {
return str_replace(\'%aec-year%\', $terms[0]->slug, $post_link);
}
}
return $post_link;
}
add_filter(\'post_type_link\', \'si0b_artists_permalinks\', 1, 2);
谢谢你的帮助。。。