我正在使用工具集类型,想知道按照我想要的方式设置URL有多容易。
我有一个自定义的帖子类型的场地和一个自定义类别分类的位置。
目前,网址像一样出现
http://domain.com/venue/location/manchester/
http://domain.com/venue/manchester/the-venue-name/
但我希望URL的结构像
http://domain.com/manchester/
http://domain.com/manchester/the-venue-name/
我需要在哪里进行这些更改?
这都是.htaccess的工作吗?或者可以在永久链接部分做些什么吗?
提前感谢
如果我理解正确,这个破解必须在你的模板中工作。首先,我们必须从urlSlug中删除Post类型名称。
function ft_remove_postType_slug_fromUrl( $post_link, $post, $leavename ) {
if ( 'venue' != $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', 'ft_remove_postType_slug_fromUrl', 10, 3 );
但这本身是行不通的。如果你在functions.php中输入了这段代码,你应该会得到404错误。B因为WordPress只希望帖子和页面以这种方式运行。
因此,您还必须添加此操作。
function ft_change_parsingRequest( $query ) {
if ( ! $query->is_main_query() || 2 != count( $query->query ) || ! isset( $query->query['page'] ) ) {
return;
}
if ( ! empty( $query->query['name'] ) ) {
$query->set( 'post_type', array( 'post', 'venue', 'page' ) );
}
}
add_action( 'pre_get_posts', 'ft_change_parsingRequest' );
之后,你更新/刷新你的post-type/permalink树(我想它叫flush_rewrites。),这意味着,在你的管理面板区域重新更新你的permalink设置。
或者,如果你想看或做一些魔术,你可以从源url中查看。
https://core.trac.wordpress.org/browser/tags/4.3/src/wp-includes/post.php#L1454
这句话说:;
add_permastruct( $post_type, "{$args->rewrite['slug']}/%$post_type%", $permastruct_args );
快乐编码。