[英]WordPress Permalinks add category name to post url
我有两种类型的帖子:自定义帖子和Wordpress常规帖子。 我希望url像这样:
/category-name/post-name/
用于Wordpress常规帖子, /post-name/
用于自定义帖子。
我能够获得自定义帖子所需的网址:
function remove_base_slug( $post_link, $post, $leavename ) {
if ( 'trip-options' != $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', 'remove_base_slug', 10, 3 );
function check_parse_request( $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', 'trip-options', 'page' ) );
}
}
add_action( 'pre_get_posts', 'check_parse_request' );
至于常规帖子,我尝试通过WP Dashboard -> Settings -> Permalinks
并将其设置为/%category%/%postname%
。 这样,它适用于常规帖子,但会破坏我的自定义帖子网址。
对我来说,两种方法都可以保留所需的网址结构吗?
所以我需要这样的东西:
if (get_post_type() === 'trip-options') {
/* URL shoud be /post-name/ */
}
else {
/* URL shoud be category/post-name */
}
您可以使用此插件为自定义帖子类型自定义帖子类型永久链接修改永久链接 。
或者,如果您想以编程方式进行操作,则可以在SO上关注这个已回答的问题
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.