我已将此脚本添加到我的网站,以便从永久链接中移除我的CPT的slu :: https://wordpress.stackexchange.com/questions/212471/remove-slug-in-the-permalink-for-more-than-one-type-of-custom-post-type
它很棒!
我现在遇到的问题是我使用子文件夹激活了多站点功能,这个脚本不再有效。
此网址曾用于:http://example.com/single-post,但现在直接转到404页面,而这是帖子网址:http://example.com/custom_post_type/single-post。
如何调整功能,以便将此特定CPT的永久链接返回到http://example.com/single-post?
function na_remove_slug( $post_link, $post, $leavename ) {
if ( 'play_states' != $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', 'na_remove_slug', 10, 3 );
function na_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', 'play_states', 'page' ) );
}
}
add_action( 'pre_get_posts', 'na_parse_request' );
或者,这可能是.htaccess问题。由于网站比子文件夹多站点的分配时间早,我不得不强制wordpress接受它。这是我目前的htaccess文件:
# BEGIN WordPress
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
# uploaded files
RewriteRule ^([_0-9a-zA-Z-]+/)?files/(.+) wp-includes/ms-files.php?file=$2 [L]
# add a trailing slash to /wp-admin
RewriteRule ^([_0-9a-zA-Z-]+/)?wp-admin$ $1wp-admin/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^[_0-9a-zA-Z-]+/(wp-(content|admin|includes).*) $1 [L]
RewriteRule ^[_0-9a-zA-Z-]+/(.*\.php)$ $1 [L]
RewriteRule . index.php [L]
# END WordPress