虽然不是您想要的确切URL结构,但您可以获得:
/products
»查看所有自定义帖子
/products/type/cell-phones
»使用taxonomy手机查看所有自定义帖子
/products/type/cell-phones/brand/samsung
»查看分类法为手机的所有自定义帖子AND 三星
/brand/samsung
»查看分类法为samsung的所有自定义帖子
/product/test-product-1
»查看产品(单个自定义帖子)
无需指定自定义重写规则。
但它确实要求您按照特定的顺序注册分类法和自定义帖子类型。诀窍是在注册自定义的post类型之前,先注册slug以post类型的slug开头的任何分类法。例如,假设以下段塞:
product_type taxonomy slug = products/type
product custom_post_type slug = product
product custom_post_type archive slug = products
product_brand taxonomy slug = brand
然后您可以按以下顺序注册它们:
register_taxonomy(
\'products_type\',
\'products\',
array(
\'label\' => \'Product Type\',
\'labels\' => $product_type_labels,
\'public\' => true,
\'show_ui\' => true,
\'show_in_nav_menus\' => true,
\'args\' => array( \'orderby\' => \'term_order\' ),
\'rewrite\' => array( \'slug\' => \'products/type\', \'with_front\' => false ),
\'has_archive\' => true,
\'query_var\' => true,
)
);
register_post_type(\'products\', array(
\'labels\' =>$products_labels,
\'singular_label\' => __(\'Product\'),
\'public\' => true,
\'show_ui\' => true,
\'capability_type\' => \'post\',
\'hierarchical\' => false,
\'rewrite\' => array(\'slug\' => \'product\', \'with_front\' => false ),
\'has_archive\' => \'products\',
\'supports\' => array(\'title\', \'editor\', \'thumbnail\', \'revisions\',\'comments\',\'excerpt\'),
));
register_taxonomy(
\'products_brand\',
\'products\',
array(
\'label\' => \'Brand\',
\'labels\' => $products_brand_labels,
\'public\' => true,
\'show_ui\' => true,
\'show_in_nav_menus\' => true,
\'args\' => array( \'orderby\' => \'term_order\' ),
\'rewrite\' => array( \'slug\' => \'brand\', \'with_front\' => false ),
\'has_archive\' => true,
\'query_var\' => true,
)
);
如果您必须有如下URL:
/products/type/cell-phones/brand/samsung/test-product-1
»查看产品(单个自定义帖子)
那么您需要这样的重写规则:
add_rewrite_rule(
\'/products/type/*/brand/*/([^/]+)/?\',
\'index.php?pagename=\'product/$matches[1]\',
\'top\' );
UPDATE https://stackoverflow.com/questions/3861291/multiple-custom-permalink-structures-in-wordpress下面是如何正确地重新定义单个帖子URL。
对于自定义帖子类型,请将re write设置为false。(保持存档不变),然后在注册分类法和帖子之后,还要注册以下重写规则。
\'rewrite\' => false
global $wp_rewrite;
$product_structure = \'/%product_type%/%brand%/%product%\';
$wp_rewrite->add_rewrite_tag("%product%", \'([^/]+)\', "product=");
$wp_rewrite->add_permastruct(\'product\', $product_structure, false);
然后过滤post\\u type\\u链接以创建所需的URL结构-允许未设置的分类值。修改链接帖子中的代码,您将有:
function product_permalink($permalink, $post_id, $leavename){
$post = get_post($post_id);
if( \'product\' != $post->post_type )
return $permalink;
$rewritecode = array(
\'%product_type%\',
\'%brand%\',
$leavename? \'\' : \'%postname%\',
$leavename? \'\' : \'%pagename%\',
);
if(\'\' != $permalink && !in_array($post->post_status, array(\'draft\', \'pending\', \'auto-draft\'))){
if (strpos($permalink, \'%product_type%\') !== FALSE){
$terms = wp_get_object_terms($post->ID, \'product_type\');
if (!is_wp_error($terms) && !empty($terms) && is_object($terms[0]))
$product_type = $terms[0]->slug;
else
$product_type = \'unassigned-artist\';
}
if (strpos($permalink, \'%brand%\') !== FALSE){
$terms = wp_get_object_terms($post->ID, \'brand\');
if (!is_wp_error($terms) && !empty($terms) && is_object($terms[0]))
$brand = $terms[0]->slug;
else
$brand = \'unassigned-brand\';
}
$rewritereplace = array(
$product_type,
$brand,
$post->post_name,
$post->post_name,
);
$permalink = str_replace($rewritecode, $rewritereplace, $permalink);
}
return $permalink;
}
add_filter(\'post_type_link\', \'product_permalink\', 10, 3);
现在,我只需要弄清楚如何在没有领先品牌标签的情况下重新编写品牌分类url,我应该精确匹配您想要的url。