如果我理解正确,您首先需要将用户重定向到http://domain.com/cpt1/%postname%/ 之后,您需要重写URL以获得正确的帖子并避免404错误。
所以你来了:
add_action( \'template_redirect\', \'redirect_postid_to_postname\' );
function redirect_postid_to_postname() {
$uri = $_SERVER[\'REQUEST_URI\'];
// Checks if the URI matches what we want. This means the URL should be sth like http://example.com/cpt1/POST_ID
if ( !preg_match( "/^\\/cpt1\\/(\\d+)/", $uri, $matches ) ) {
return;
}
$post_id = $matches[1];
// Checks if $post_id is actually an existent post ID.
$post_obj = get_post( $post_id );
// Checks if a post object was retrieved for the given post ID.
if ( !$post_obj ) {
return;
}
$post_name = $post_obj->post_name; // Keeps post slug.
$site_url = get_site_url(); // Keeps site URL (it\'s supposed to be retrieved without trailing slash).
$location = $site_url . "/cpt1/$post_name"; // Builds the new URL to redirect the user to.
// Redirects!
wp_redirect( $location, 302 );
exit;
}
add_action( \'init\', \'rewrite_custom_postname\', 100 );
function rewrite_custom_postname() {
$uri = $_SERVER[\'REQUEST_URI\'];
// Checks if the URI matches what we want. This means the URL should be sth like http://example.com/cpt1/POST_NAME
if ( !preg_match( "/^\\/cpt1\\/(.+)/", $uri, $matches ) ) {
return;
}
$post_name = $matches[1];
$query_args = array(
\'name\' => $post_name,
\'post_type\' => \'cpt1\',
\'post_status\' => \'publish\',
\'numberposts\' => 1
);
$post_obj = get_posts( $query_args );
// Checks if a post object was retrieved for the given query arguments.
if ( !$post_obj ) {
return;
}
$post_id = $post_obj[0]->ID;
$regex = "^cpt1/{$matches[1]}";
$query = "index.php?p=$post_id";
$after = "top";
add_rewrite_rule( $regex, $query, $after );
}
IMPORTANT!如果您仍在使用Permalink管理器,并且还将uri从%postname%更改为%post\\u id%,则可能会创建无限的重定向循环,因此请小心。