我想要实现的是一个具有如下URL的在线列表目录:
www.example.com/drawing-tools/brand/type/material/color/
在本例中,我有一个名为“drawing tools”的自定义帖子类型,其中包含4个自定义分类:“brand”、“type”、“material”、“color”
当用户访问URL时,如:
www.example.com/drawing-tools/rotring/mechanical-pencil/plastic/black/
它获取所有符合这些条件的项。
棘手的是,所有分类法都必须位于URL中。比如:
www.example.com/drawing-tools/plastic/black/
只会返回404错误页。
所以我的主要问题是:
我怎样才能将URL中的所有分类法都设置为该格式,并在缺少一个分类法的情况下返回404错误页?
我在考虑编写一个重写规则,将这些uri段作为参数传递给基于\'Query Multiple Taxonomies\' 插件,如果缺少一个段,则返回404错误。但我不知道如何做到这一点。你能给我什么解决方案吗?
[Update 1]
重写规则基本上采用以下URL:
www.example.com/drawing-tools/rotring/mechanical-pencil/plastic/black/
并将其制成:
www.example.com/?drawing-tools_name=rotring+mechanical-pencil+plastic+black
上述结构是
\'Query Multiple Taxonomies\' 插件。
[Update 2]
我已经放弃了Query Multiple Taxonomies插件,转而使用WP 3.1。现在,在这方面的帮助下
previous question 我已经按照自己的意愿着手研究这些分类法。
以下是完整代码:
register_post_type(\'drawing-tools\', array(
\'labels\' => array(
\'name\' => __( \'Drawing Tools\' ),
\'singular_name\' => __( \'Drawing Tool\' ),
\'add_new\' => __( \'Add New\' ),
\'add_new_item\' => __( \'Add New Drawing Tool\' ),
\'edit\' => __( \'Edit\' ),
\'edit_item\' => __( \'Edit Drawing Tool\' ),
\'new_item\' => __( \'New Drawing Tool\' ),
\'view\' => __( \'View Drawing Tools\' ),
\'view_item\' => __( \'View Drawing Tool\' ),
\'search_items\' => __( \'Search Drawing Tools\' ),
\'not_found\' => __( \'No items found\' ),
\'not_found_in_trash\' => __( \'No items found in trash\' ),
\'parent\' => __( \'Parent Drawing Tool\' ),
),
\'public\' => true,
\'publicly_queryable\' => false,
\'show_in_nav_menus\' => false,
\'exclude_from_search\' => false,
\'show_ui\' => true,
\'_builtin\' => false,
\'_edit_link\' => \'post.php?post=%d\',
\'capability_type\' => \'post\',
\'hierarchical\' => false,
\'rewrite\' => array("slug" => "drawing-tools/%brand%/%type%/%material%/%color%"), // Permalinks format
\'has_archive\' => false,
\'menu_position\' => 5,
\'supports\' => array(\'author\')
));
register_taxonomy(\'brand\',array(\'drawing-tools\'), array(
\'hierarchical\' => false,
\'labels\' => array(
\'name\' => _x( \'Brands\', \'taxonomy general name\' ),
\'singular_name\' => _x( \'Brand\', \'taxonomy singular name\' ),
\'search_items\' => __( \'Search Brands\' ),
\'all_items\' => __( \'All Brands\' ),
\'parent_item\' => __( \'Parent Brand\' ),
\'parent_item_colon\' => __( \'Parent Brand:\' ),
\'edit_item\' => __( \'Edit Brands\' ),
\'update_item\' => __( \'Update Brand\' ),
\'add_new_item\' => __( \'Add New Brand\' ),
\'new_item_name\' => __( \'New Unit Brand\' ),
\'menu_name\' => __( \'Brands\' ),
),
\'show_ui\' => true,
\'query_var\' => true,
\'rewrite\' => array( \'slug\' => \'drawing-tools\' ),
));
register_taxonomy(\'type\',array(\'drawing-tools\'), array(
\'hierarchical\' => false,
\'labels\' => array(
\'name\' => _x( \'Types\', \'taxonomy general name\' ),
\'singular_name\' => _x( \'Type\', \'taxonomy singular name\' ),
\'search_items\' => __( \'Search Types\' ),
\'all_items\' => __( \'All Types\' ),
\'parent_item\' => __( \'Parent Type\' ),
\'parent_item_colon\' => __( \'Parent Type:\' ),
\'edit_item\' => __( \'Edit Types\' ),
\'update_item\' => __( \'Update Type\' ),
\'add_new_item\' => __( \'Add New Type\' ),
\'new_item_name\' => __( \'New Type\' ),
\'menu_name\' => __( \'Types\' ),
),
\'show_ui\' => true,
\'query_var\' => true,
\'rewrite\' => array( \'slug\' => \'drawing-tools/%brand%\' ),
));
register_taxonomy(\'material\',array(\'drawing-tools\'), array(
\'hierarchical\' => false,
\'labels\' => array(
\'name\' => _x( \'Materials\', \'taxonomy general name\' ),
\'singular_name\' => _x( \'Material\', \'taxonomy singular name\' ),
\'search_items\' => __( \'Search Materials\' ),
\'all_items\' => __( \'All Materials\' ),
\'parent_item\' => __( \'Parent Material\' ),
\'parent_item_colon\' => __( \'Parent Material:\' ),
\'edit_item\' => __( \'Edit Material\' ),
\'update_item\' => __( \'Update Material\' ),
\'add_new_item\' => __( \'Add New Material\' ),
\'new_item_name\' => __( \'New Material Name\' ),
\'menu_name\' => __( \'Materials\' ),
),
\'show_ui\' => true,
\'query_var\' => true,
\'rewrite\' => array( \'slug\' => \'drawing-tools/%brand%/%type%\' ),
));
register_taxonomy(\'color\',array(\'drawing-tools\'), array(
\'hierarchical\' => false,
\'labels\' => array(
\'name\' => _x( \'Colors\', \'taxonomy general name\' ),
\'singular_name\' => _x( \'Color\', \'taxonomy singular name\' ),
\'search_items\' => __( \'Search Colors\' ),
\'all_items\' => __( \'All Colors\' ),
\'parent_item\' => __( \'Parent Color\' ),
\'parent_item_colon\' => __( \'Parent Color:\' ),
\'edit_item\' => __( \'Edit Color\' ),
\'update_item\' => __( \'Update Color\' ),
\'add_new_item\' => __( \'Add New Color\' ),
\'new_item_name\' => __( \'New Color Name\' ),
\'menu_name\' => __( \'Colors\' ),
),
\'show_ui\' => true,
\'query_var\' => true,
\'rewrite\' => array( \'slug\' => \'drawing-tools/%brand%/%type%/%material%\' ),
));
function filter_post_type_link($link, $post)
{
if ($post->post_type != \'drawing-tools\')
return $link;
if ($cats = get_the_terms($post->ID, \'brand\'))
$link = str_replace(\'%brand%\', array_pop($cats)->slug, $link);
if ($cats = get_the_terms($post->ID, \'type\'))
$link = str_replace(\'%type%\', array_pop($cats)->slug, $link);
if ($cats = get_the_terms($post->ID, \'material\'))
$link = str_replace(\'%material%\', array_pop($cats)->slug, $link);
if ($cats = get_the_terms($post->ID, \'color\'))
$link = str_replace(\'%color%\', array_pop($cats)->slug, $link);
return $link;
}
add_filter(\'post_type_link\', \'filter_post_type_link\', 10, 2);
现在,当用户访问链接时:
www.example.com/drawing-tools/rotring/mechanical-pencil/plastic/black/
一切正常。问题是用户也可以访问这样的部分结构
www.example.com/drawing-tools/rotring/
根据这些分类法仍然可以得到结果。
如何限制用户访问此类URL,而不是返回404错误消息?