虽然不是您想要的确切URL结构,但您可以获得:
/产品
»查看所有自定义帖子
/产品/类型/手机
»使用分类手机查看所有自定义帖子
/产品/类型/手机/品牌/三星
»查看所有分类为手机和三星的自定义帖子
/ brand / samsung
»查看分类法为三星的所有自定义帖子
/ product / test-product-1
»查看产品(单个自定义帖子)
无需指定自定义重写规则。
确实要求您以特定顺序注册分类法和自定义帖子类型。诀窍是在注册自定义帖子类型之前,先注册任何以该帖子类型的文档开头的文档。例如,假定以下块:
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,
)
);
如果您绝对必须具有以下网址:
/ products / type / cells / phones / brand / samsung / test-product-1
»查看产品(单个自定义帖子)
然后,您将需要类似以下内容的重写规则:
add_rewrite_rule(
'/products/type/*/brand/*/([^/]+)/?',
'index.php?pagename='product/$matches[1]',
'top' );
更新
/programming/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_type_link以创建所需的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);
现在,我只需要弄清楚如何在没有前导品牌标签的情况下重写品牌分类网址,并且我应该准确匹配您所需的网址。