我被重写规则弄糊涂了,需要帮助。
我创建了一个带有自定义元“brand”的自定义帖子类型“products”。
名为milk的产品的URL将为mysite.com/products/milk
, 我需要URL包括这样的品牌,mysite.com/brand-brandname/milk
Some examples:mysite.com/brand-supermilk/milk
其中“supermilk”是牛奶定制元数据中的品牌
mysite.com/brand-goodproducts/coffe
好产品就是品牌
我有这个规则并重写:
$args = array(
\'rewrite\' => array(\'slug\' => \'brand-%brand_name%\', \'with_front\' => false),
);
register_post_type(\'products\', $args);
function wptuts_custom_tags() {
add_rewrite_rule("^brand-([^/]+)?",\'index.php?post_type=products&brand_name=$matches[1]\',\'top\');
flush_rewrite_rules();
}
add_action(\'init\',\'wptuts_custom_tags\');
function my_post_type_link_filter_function($post_link, $id = 0, $leavename = FALSE) {
if (strpos(\'%brand_name%\', $post_link) === FALSE) {
$post = &get_post($id);
$brand_name =get_post_meta($post->ID,\'brand\',true);
if(empty($brand_name)){$brand_name = \'default\';}
$post_link = str_replace(\'%brand_name%\',$brand_name, $post_link);
return $post_link;
}
}
add_filter(\'post_type_link\', \'my_post_type_link_filter_function\', 1, 3);
当我添加新产品帖子时,所有这些都会在管理员中显示正确的URL,但会显示404或帖子的产品列表,但不会显示产品页面。