基本上,我想使用自定义帖子类型实现一个词汇表,并且在设置重写时遇到一些问题。我想要这样:
主要词汇表URL:http://example.com/glossary/
以字母开头的词汇表术语A:http://example.com/glossary/a/
单个词汇表术语的URL:http://example.com/glossary/a/atomic/
实际上,我使用下面的代码实现了这一点,但我确信这是一种非常笨拙的方式,而且我知道它在某些地方出现了故障,因为在查看页面时调用了错误的模板。除了http://example.com/glossary/, 在哪里存档相扑词汇表术语。php按预期调用,其他两个只激活索引。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\');