WordPress的自定义帖子类型和分类网址

问题描述:

我做了一个自定义的帖子类型和分类,以便我可以有一个存档页面的结构/集,将显示所有的孩子“条款”,/ episodes/custom-term来显示特定字词档案,以及/剧集/定制字幕/后期字幕以显示单个帖子。WordPress的自定义帖子类型和分类网址

我能够为我创建的任何自定义术语获取/剧集/自定义术语。不过,我收到404个/集以及404集/集/定制/后期标题。

在CMS中,当我发布帖子时,假设我的自定义术语是第一季,并且帖子标题是“样本插曲”。您可以前往/ episodes/season-one,它将使用“taxonomy-episode_category.php”模板输出第一季中的所有帖子。它也知道创建帖子的时候,永久链接是/ episodes/season-one/sample-episode,但它会达到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')) { 

    $link = str_replace('%episode_cat%', array_pop($cats)->slug, $link); 
    return $link; 
} 
} 
add_filter('post_type_link', 'filter_post_type_link', 10, 2); 
+0

您是否找到了该问题的答案? – dclawson 2015-06-29 20:46:40

+0

请参阅下面的答案 – James 2015-06-30 21:49:28

当我将永久链接结构设置为默认而不是Post名称时,原来的分页工作正常。要使用帖子名称,我可以使用精确的代码以及一个新功能:

function add_rewrite_rules() { 
    add_rewrite_rule('episodes/(.+?)/page/?([0-9]{1,})/?$', 'index.php', 'top'); 
} 
add_filter('init', 'add_rewrite_rules')