自定义分类法和自定义帖子类型使用相同的重写结构是否有问题?
我有一个自定义分类法people
和自定义帖子类型people_bio
. 这样做的目的是,你可以得到一个关于一个人的帖子列表,页面顶部有一个简短的传记。我在我的taxonomy-people.php
模板文件。permalink是/people/[person-slug]
.
自定义分类法和自定义帖子类型都具有rewrite
参数设置为array(\'slug\' => \'people\')
. 这似乎有效:get_term_link(\'seth-godin\', \'people\')
退货/people/seth-godin/
, 对于带有slug的自定义post类型seth-godin
, get_permalink()
还返回/people/seth-godin/
. 首先定义了分类法,它似乎“获胜”:在/people/[slug]
页is_tax()
是真的,而is_single()
为false。
所以,这很有效,但我觉得不舒服。有没有人对重写引擎更有经验,你能告诉我这是否会破坏其他东西吗?
插件文件的相关部分,在init
措施:
register_taxonomy(
\'people\',
\'post\',
array(
\'labels\' => array(
\'name\' => \'People\',
\'singular_name\' => \'Person\',
\'search_items\' => \'Search people\',
\'popular_items\' => \'Popular people\',
\'all_items\' => \'All people\',
\'parent_item\' => null,
\'parent_item_colon\' => null,
\'edit_item\' => \'Edit person\',
\'update_item\' => \'Update person\',
\'add_new_item\' => \'Add person\',
\'new_item_name\' => \'New person\',
\'separate_items_with_commas\' => \'Separate people with commas\',
\'add_or_remove_items\' => \'Add or remove people\',
\'choose_from_most_used\' => \'Choose from the most used people\',
),
\'public\' => true,
\'show_ui\' => true,
\'show_tagcloud\' => true,
\'hierarchical\' => false,
\'update_count_callback\' => \'\',
\'rewrite\' => array(
\'slug\' => \'people\',
\'with_front\' => true,
),
\'query_var\' => \'people\',
\'capabilities\' => array(),
\'show_in_nav_menus\' => true,
)
);
register_post_type(
\'people_bio\',
array(
\'label\' => \'People Bio\',
\'labels\' => array(
\'name\' => \'Biographies\',
\'singular_name\' => \'Biography\',
\'add_new\' => \'Add new\',
\'add_new_item\' => \'Add new biography\',
\'edit_item\' => \'Edit biography\',
\'new_item\' => \'New biography\',
\'view_item\' => \'View biography\',
\'search_items\' => \'Search biographies\',
\'not_found\' => \'No biographies found\',
\'not_found_in_trash\' => \'No biographies found in trash\',
\'parent_item_colon\' => null,
),
\'description\' => \'Biography pages of interesting people\',
\'public\' => true,
\'exclude_from_search\' => true,
\'publicly_queryable\' => true,
\'show_ui\' => true,
\'menu_position\' => null,
\'menu_icon\' => null,
\'capability_type\' => \'post\',
\'capabilities\' => array(),
\'hierarchical\' => false,
\'supports\' => array(
\'title\',
\'editor\',
//\'author\',
\'thumbnail\',
\'excerpt\',
//\'trackbacks\',
\'custom-fields\',
//\'comments\',
//\'revisions\',
//\'page-attributes\',
),
\'register_meta_box_cb\' => null,
\'taxonomies\' => array(),
\'permalink_epmask\' => EP_PERMALINK,
//\'rewrite\' => false,
\'rewrite\' => array(
\'slug\' => \'people\',
\'with_front\' => true,
),
\'query_var\' => true,
\'can_export\' => true,
\'show_in_nav_menus\' => true,
)
);
register_taxonomy_for_object_type(\'people\', \'people_bio\');
(我总是将所有参数与register_*()
, 许多都有其默认值,作为额外的文档,只要Codex不是最新的)
The
taxonomy-people.php
模板文件:
<?php
get_header();
$people_biography = get_posts(array(
\'numberposts\' => -1,
\'post_type\' => \'people_bio\',
\'taxonomy\' => \'people\',
\'term\' => $wp_query->get_queried_object()->slug,
));
?>
<div class="container_24">
<div class="grid_18" id="content" role="main">
<?php if ($people_biography) :
foreach ($people_biography as $bio) : ?>
<h1><?php echo get_the_title($bio->ID); ?></h1>
<?php
echo get_the_post_thumbnail($bio->ID);
echo apply_filters(\'the_content\', $bio->post_content); ?>
<?php
endforeach;
else: ?>
<h1><?php esc_html_e($wp_query->get_queried_object()->name); ?></h1>
<?php endif; ?>
<?php get_template_part( \'loop\', \'archive\' ); ?>
</div><!-- .content -->
<div class="grid_6" id="default_sidebar">
<?php dynamic_sidebar(\'default-sidebar\'); ?>
</div><!-- #default_sidebar -->
</div><!-- .container_24 -->
<div class="clear"></div>
<?php
get_footer();
?>
更新:生成的重写规则
Rewrite Analyzer 似乎告诉我,对于常规的分类法页面,分类法“获胜”(我注意到了这一点),但自定义的post类型会获得所有其他URL(包括第二页、提要等)。这就是我所害怕的,我需要进一步调查。