根据maioman发布的链接,我对以下问题进行了修改:
我很难使用自定义的帖子类型/分类法。我想要一个自定义的帖子类型的剧集,并且自定义帖子类型的分类是包含用户生成的术语的剧集。
我能够
剧集/自定义术语名称/工作。如果你转到这里,它会显示与此术语相关的所有帖子。
然而,如果你在剧集/自定义术语名称/帖子标题上看到一篇帖子,我会看到404页。
function episodes() {
$labels = array(
\'name\' => _x( \'Episodes\', \'Post Type General Name\', \'text_domain\' ),
\'singular_name\' => _x( \'Episodes\', \'Post Type Singular Name\', \'text_domain\' ),
\'menu_name\' => __( \'Episodes\', \'text_domain\' ),
\'parent_item_colon\' => __( \'Parent Item:\', \'text_domain\' ),
\'all_items\' => __( \'All Episodes\', \'text_domain\' ),
\'view_item\' => __( \'View Item\', \'text_domain\' ),
\'add_new_item\' => __( \'Add New Item\', \'text_domain\' ),
\'add_new\' => __( \'Add new episode\', \'text_domain\' ),
\'edit_item\' => __( \'Edit Item\', \'text_domain\' ),
\'update_item\' => __( \'Update Item\', \'text_domain\' ),
\'search_items\' => __( \'Search Item\', \'text_domain\' ),
\'not_found\' => __( \'Not found\', \'text_domain\' ),
\'not_found_in_trash\' => __( \'Not found in Trash\', \'text_domain\' )
);
$rewrite = array(
\'slug\' => \'episodes/%episode_cat%\',
\'with_front\' => false,
\'pages\' => true,
\'feeds\' => true
);
$args = array(
\'label\' => __( \'episodes\', \'text_domain\' ),
\'description\' => __( \'All episodes\', \'text_domain\' ),
\'labels\' => $labels,
\'supports\' => array(\'title\' ),
\'hierarchical\' => true,
\'public\' => true,
\'show_ui\' => true,
\'show_in_menu\' => true,
\'show_in_nav_menus\' => true,
\'show_in_admin_bar\' => true,
\'menu_position\' => 5,
\'can_export\' => true,
\'has_archive\' => true,
\'exclude_from_search\' => false,
\'publicly_queryable\' => true,
\'rewrite\' => $rewrite,
\'capability_type\' => \'page\',
\'query_var\' => true,
\'_builtin\' => false
);
register_post_type( \'episodes_listing\', $args );
}
add_action( \'init\', \'episodes\', 0 );
function episodes_taxomony() {
$labels = array(
\'name\' => _x( \'Episodes Categories\', \'Taxonomy General Name\', \'text_domain\' ),
\'singular_name\' => _x( \'Episodes\', \'Taxonomy Singular Name\', \'text_domain\' ),
\'menu_name\' => __( \'Episode Categories\', \'text_domain\' ),
\'all_items\' => __( \'All Items\', \'text_domain\' ),
\'parent_item\' => __( \'Parent Item\', \'text_domain\' ),
\'parent_item_colon\' => __( \'Parent Item:\', \'text_domain\' ),
\'new_item_name\' => __( \'New Item Name\', \'text_domain\' ),
\'add_new_item\' => __( \'Add new episode\', \'text_domain\' ),
\'edit_item\' => __( \'Edit Item\', \'text_domain\' ),
\'update_item\' => __( \'Update Item\', \'text_domain\' ),
\'separate_items_with_commas\' => __( \'Separate items with commas\', \'text_domain\' ),
\'search_items\' => __( \'Search Items\', \'text_domain\' ),
\'add_or_remove_items\' => __( \'Add or remove items\', \'text_domain\' ),
\'choose_from_most_used\' => __( \'Choose from the most used items\', \'text_domain\' ),
\'not_found\' => __( \'Not Found\', \'text_domain\' )
);
$rewrite = array(
\'slug\' => \'episodes\',
\'with_front\' => false,
\'hierarchical\' => true
);
$args = array(
\'labels\' => $labels,
\'hierarchical\' => true,
\'public\' => true,
\'show_ui\' => true,
\'show_admin_column\' => true,
\'show_in_nav_menus\' => true,
\'show_tagcloud\' => true,
\'query_var\' => true,
\'rewrite\' => $rewrite
);
register_taxonomy( \'episodes_category\', array(\'episodes_listing\'), $args );
}
add_action( \'init\', \'episodes_taxomony\', 0 );
function filter_post_type_link($link, $post)
{
if ($post->post_type != \'episodes_listing\')
return $link;
if ($cats = get_the_terms($post->ID, \'episodes_category\')) {
//echo \'This is the \'. $link;
$link = str_replace(\'%episode_cat%\', array_pop($cats)->slug, $link);
return $link;
}
}
add_filter(\'post_type_link\', \'filter_post_type_link\', 10, 2);
为了解决上述问题,我加入了
add_filter(\'rewrite_rules_array\', \'mmp_rewrite_rules\');
function mmp_rewrite_rules($rules) {
$newRules = array();
$newRules[\'episodes/(.+)/(.+)/(.+)/(.+)/?$\'] = \'index.php?custom_post_type_name=$matches[3]\'; // my custom structure will always have the post name as the 5th uri segment
$newRules[\'episodes/(.+)/?$\'] = \'index.php?taxonomy_name=$matches[1]\';
return array_merge($newRules, $rules);
}
但现在/剧集/自定义术语名称将我带到主页/剧集/自定义术语名称/帖子标题将我带到主页
所以,什么都不管用。
我已尝试更改$matches数组编号。
注意:在我的WP阅读设置中,我将其设置为静态页面。当我调用mmp\\u rewrite\\u rules函数时,会显示这个静态页面。