WordPress自定义帖子类型和分类URL

时间:2014-09-10 15:01:45

标签: wordpress custom-post-type permalinks taxonomy custom-taxonomy

我制作了一个自定义的帖子类型和分类,这样我就可以为一个存档页面设置一个/ episodes结构,显示所有子项“terms”,/ episodes / custom-term来显示特定术语的存档,以及/ episodes / custom-term / post-title显示单个帖子。

我能够为我创建的任何自定义术语/ episodes / custom-term工作。但是,我正在收到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);

1 个答案:

答案 0 :(得分:1)

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

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