gpt4 book ai didi

wordpress - 如何从自定义帖子类型 url 中删除分类 slug?

转载 作者:行者123 更新时间:2023-12-03 07:55:44 24 4
gpt4 key购买 nike

我有一个带有分类的自定义帖子类型(产品) 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。

任何人都可以帮助我吗?

最佳答案

我认为您忘记重写自定义分类法 post slug。
将此写在您的 register_post_type方法。

'rewrite' => array('slug' => 'product-type')

现在你必须删除 product-type从您的定制产品中提取
/**
* 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);

现在,由于您已删除自定义帖子类型 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');

引用: Remove The Slugs from Custom Post Type URL

希望这可以帮助!

关于wordpress - 如何从自定义帖子类型 url 中删除分类 slug?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41230665/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com