Dev*_*ley 5 wordpress url-rewriting taxonomy custom-post-type
我有一个带有分类法的自定义帖子类型(产品)product-type。我的网址之一是这样的:
http : //www.naturesbioscience.com/product-type/immune-support-supplements/
我想要这样的:http :
//www.naturesbioscience.com/immune-support-supplements/
我"rewrite" => array('slug' => '/ ', 'with_front' => false在register_taxonomy函数中使用过,我得到的网址如下:
http : //www.naturesbioscience.com/immune-support-supplements/
但我在其他页面中找不到 404。
任何人都可以帮助我吗?
我认为您忘记重写自定义分类法帖子。
把这个写在你的register_post_type方法中。
'rewrite' => array('slug' => 'product-type')
Run Code Online (Sandbox Code Playgroud)
现在您必须product-type从定制产品中删除slug
/**
* Remove the slug from published post permalinks.
*/
function custom_remove_cpt_slug($post_link, $post, $leavename)
{
if ('product-type' != $post->post_type || 'publish' != $post->post_status)
{
return $post_link;
}
$post_link = str_replace('/' . $post->post_type . '/', '/', $post_link);
return $post_link;
}
add_filter('post_type_link', 'custom_remove_cpt_slug', 10, 3);
Run Code Online (Sandbox Code Playgroud)
现在,由于您已经删除了自定义帖子类型 slug,因此 WordPress 会尝试将其与页面或帖子匹配,因此您必须告诉 WP 也检查您的自定义帖子类型中的 URL。所以用这个:
function custom_parse_request_tricksy($query)
{
// Only noop the main query
if (!$query->is_main_query())
return;
// Only noop our very specific rewrite rule match
if (2 != count($query->query) || !isset($query->query['page']))
{
return;
}
// 'name' will be set if post permalinks are just post_name, otherwise the page rule will match
if (!empty($query->query['name']))
{
$query->set('post_type', array('post', 'product-type', 'page'));
}
}
add_action('pre_get_posts', 'custom_parse_request_tricksy');
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助!
| 归档时间: |
|
| 查看次数: |
14593 次 |
| 最近记录: |