基本上,我想使用自定义帖子类型来创建词汇表,并在设置重写我希望的方式时遇到一些问题。我想要这样:
主要词汇表URL:
http://example.com/glossary/
术语以字母A开头:
http://example.com/glossary/a/
单个词汇表术语的网址:
http://example.com/glossary/a/atomic/
我实际上是使用下面的代码实现的,但是我确信这是一种非常尴尬的方式,而且我知道它在某处发生了故障,因为查看页面时调用了错误的模板。除了http://example.com/glossary/之外,archive-sumo-glossary-term.php的调用符合预期,另外两个只是激活了我主题中的index.php。
这是(functions.php
主题中):
add_action('init', 'create_glossary');
function create_glossary()
{
register_post_type
(
'sumo-glossary-term',
array
(
'labels' => array
(
'name' => _x('Glossary Terms', 'post type general name'),
'singular_name' => _x('Glossary Term', 'post type singular name')
# And so on …
),
'supports' => array('title', 'editor', 'thumbnail'),
'public' => true,
'rewrite' => array
(
'slug' => 'glossary',
'with_front' => false
),
'query_var' => 'glossary-term',
'has_archive' => true
)
);
register_taxonomy
(
'sumo-glossary-letter',
'sumo-glossary-term',
array
(
'hierarchical' => true,
'labels' => array
(
'name' => _x('Letters', 'taxonomy general name'),
'singular_name' => _x('Letter', 'taxonomy singular name')
# And so one
),
'show_ui' => true,
'query_var' => 'glossary-letter',
'rewrite' => false
)
);
}
add_filter('post_type_link', 'glossary_term_permalink', 10, 4);
function glossary_term_permalink($post_link, $post, $leavename, $sample)
{
if ($post->post_type == 'sumo-glossary-term')
{
$permalink = str_replace('glossary/', 'glossary/' . $post->post_name[0] . '/', $post_link);
}
return $permalink;
}
add_rewrite_rule('^glossary/([^/]*)?$','index.php?glossary-letter=$matches[1]','top');
add_rewrite_rule('^glossary/([^/]*)/([^/]*)?$','index.php?glossary-term=$matches[2]','top');