CPT 中的 WordPress 分类 URL
WordPress taxonomy in CPT URL
我创建了一个带有分类法的 CPT,详细信息页面的当前 URL 是 /items/test
,其中“test”是 CPT 项目的名称。现在,我已经使用 ACF 设置了分类法(因此我可以将其限制为单一分类法),尽管它也可能是通过 WordPress 侧边栏本地设置的。
现在我希望 URL 将 URL 更改为类似 /items/waterproof/test
的内容,其中“测试”仍然是项目名称,“防水”是选定的分类法,我图我必须为此使用重写,但我已经尝试了一段时间但似乎无法弄清楚
有什么建议吗?
我最终使用了来自不同 Whosebug post 的 this 答案,感谢@Chengmin 留下这个建议!希望我自己找到它 ;)
CustomPostTypeHelper::registerPostType(\Entity\Product::POST_TYPE_NAME, [
'labels' => [
'name' => 'Producten',
'singular_name' => 'Product',
'all_items' => 'Alle producten',
'edit_item' => 'Product bewerken',
'add_new' => 'Nieuw Product',
'add_new_item' => 'Product toevoegen'
],
'publicly_queryable' => true,
'exclude_from_search' => false,
'show_ui' => true,
'show_in_nav_menus' => true,
'show_in_menu' => true,
'show_in_admin_bar' => true,
'public' => true,
'has_archive' => true,
'menu_icon' => 'dashicons-products',
'supports' => [
'title',
'editor'
],
'rewrite' => [
'slug' => 'verkoop/%taxonomy%'
]
]);
这是我的 CPT,我们应该注意到我已经将 register_post_type
函数包装在自定义函数中,但我仍然使用相同的参数,所以只关注它。相关信息是我更改为 verkoop/%taxononmy%
的重写值,其中“verkoop”是固定前缀,“%taxonomy%”是(你猜对了)我想要嵌入 URL 的分类法。
function productLink($link, $id = 0)
{
$post = get_post($id);
if (is_object($post)) {
$terms = wp_get_object_terms($post->ID, 'category_product_category');
if ($terms) return str_replace( '%taxonomy%', $terms[0]->slug, $link );
}
return $link;
}
add_filter('post_type_link', 'productLink', 1, 3);
这是在创建或编辑 CPT 实例时用于生成 URL 的 PHP。正如您在代码片段中所见:我的分类法名为“category_product_category”
function producteRewrite()
{
add_rewrite_rule(
'^verkoop/(.*)/(.*)/?$',
'index.php?post_type=product&name=$matches[2]',
'top'
);
}
add_action('init', 'producteRewrite');
最后但同样重要的是,这是将用户重定向到正确的详细信息页面的重写规则,我实际上并没有根据分类法进行查询,只是使用了 post slug。我的客户只想要这个用于 SEO(和个人偏好),所以我不关心基于分类的过滤
我创建了一个带有分类法的 CPT,详细信息页面的当前 URL 是 /items/test
,其中“test”是 CPT 项目的名称。现在,我已经使用 ACF 设置了分类法(因此我可以将其限制为单一分类法),尽管它也可能是通过 WordPress 侧边栏本地设置的。
现在我希望 URL 将 URL 更改为类似 /items/waterproof/test
的内容,其中“测试”仍然是项目名称,“防水”是选定的分类法,我图我必须为此使用重写,但我已经尝试了一段时间但似乎无法弄清楚
有什么建议吗?
我最终使用了来自不同 Whosebug post 的 this 答案,感谢@Chengmin 留下这个建议!希望我自己找到它 ;)
CustomPostTypeHelper::registerPostType(\Entity\Product::POST_TYPE_NAME, [
'labels' => [
'name' => 'Producten',
'singular_name' => 'Product',
'all_items' => 'Alle producten',
'edit_item' => 'Product bewerken',
'add_new' => 'Nieuw Product',
'add_new_item' => 'Product toevoegen'
],
'publicly_queryable' => true,
'exclude_from_search' => false,
'show_ui' => true,
'show_in_nav_menus' => true,
'show_in_menu' => true,
'show_in_admin_bar' => true,
'public' => true,
'has_archive' => true,
'menu_icon' => 'dashicons-products',
'supports' => [
'title',
'editor'
],
'rewrite' => [
'slug' => 'verkoop/%taxonomy%'
]
]);
这是我的 CPT,我们应该注意到我已经将 register_post_type
函数包装在自定义函数中,但我仍然使用相同的参数,所以只关注它。相关信息是我更改为 verkoop/%taxononmy%
的重写值,其中“verkoop”是固定前缀,“%taxonomy%”是(你猜对了)我想要嵌入 URL 的分类法。
function productLink($link, $id = 0)
{
$post = get_post($id);
if (is_object($post)) {
$terms = wp_get_object_terms($post->ID, 'category_product_category');
if ($terms) return str_replace( '%taxonomy%', $terms[0]->slug, $link );
}
return $link;
}
add_filter('post_type_link', 'productLink', 1, 3);
这是在创建或编辑 CPT 实例时用于生成 URL 的 PHP。正如您在代码片段中所见:我的分类法名为“category_product_category”
function producteRewrite()
{
add_rewrite_rule(
'^verkoop/(.*)/(.*)/?$',
'index.php?post_type=product&name=$matches[2]',
'top'
);
}
add_action('init', 'producteRewrite');
最后但同样重要的是,这是将用户重定向到正确的详细信息页面的重写规则,我实际上并没有根据分类法进行查询,只是使用了 post slug。我的客户只想要这个用于 SEO(和个人偏好),所以我不关心基于分类的过滤