【发布时间】:2019-02-23 11:17:21
【问题描述】:
干杯,
出于 SEO 原因,我尝试删除自定义分层服务帖子类型的基本 slug(示例名称:“custom-service”)。这些方法称为 SEO 筒仓。我尝试了不同的方法来获得以下 url 结果。
它不应该是什么样子:
https://foo.bar/custom-service/seo-agency/
https://foo.bar/custom-service/seo-agency/seo-workshop/
https://foo.bar/custom-service/foobar-agency/
https://foo.bar/custom-service/foobar-agency/foobar-workshop/
它应该是什么样子:
https://foo.bar/seo-agency/
https://foo.bar/seo-agency/seo-workshop/
https://foo.bar/foobar-agency/
https://foo.bar/foobar-agency/foobar-workshop/
我使用以下教程来实现这些结构:
- https://wordpress.stackexchange.com/questions/114723/removing-base-slug-from-hierarchical-custom-post-type
- https://kellenmace.com/remove-custom-post-type-slug-from-permalinks/
有些人有同样的问题:
- Remove base slug in permalinks of hierarchical custom post type
- https://wordpress.stackexchange.com/questions/159950/remove-base-slug-from-hierarchical-custom-post-type?rq=1
起初,我实施了 Matthew Boynes 的第一个教程的解决方案。一切正常,除了我的分页。如果我使用这些解决方案,我的分页将停止工作。
函数add_rewrites() 中的以下代码可能会破坏分页:
add_permastruct( 'custom-service', "%custom-service%", array(
'ep_mask' => EP_PERMALINK
));
你不能调用我博客的页面。以下网址停止工作:
https://foo.bar/blog/page/2
https://foo.bar/cpt/page/2
在第二次尝试中,我删除了 Matthew Boynes 代码。在其存档中没有分页的博客是没有用的。然后我实施了来自“kellenmace.com”的解决方案。要使代码适用于分层帖子类型,您必须修改 post_type_link 的教程过滤功能:
function remove_custom_service_slug( $post_link, $post ) {
if ( 'custom-service' === $post->post_type && 'publish' === $post->post_status ) {
if( $post->post_parent ) {
$parent = get_post($post->post_parent);
$post_link = str_replace( '/' . $post->post_type . '/' . $parent->post_name . '/', '/', $post_link );
} else {
$post_link = str_replace( '/' . $post->post_type . '/', '/', $post_link );
}
}
return $post_link;
}
add_filter( 'post_type_link', 'remove_custom_service_slug', 10, 2 );
但是有一个大问题。它删除了基础 slug 和父服务 slug:
https://foo.bar/seo-agency/
https://foo.bar/seo-workshop/
这有点令人沮丧。也许有人有删除分层帖子类型的基本 slug 的经验,并且对我的项目有很好的提示。
如果您有任何问题,或者如果您想了解更多信息或代码,请随时与我联系。
感谢您阅读我的问题!
【问题讨论】:
标签: php wordpress url-rewriting permalinks