综观这里的答案,我认为有一个更好的解决方案的空间,它结合了我在上面学到的一些东西,并添加了自动检测和防止重复的post段塞。
注意:在下面的示例中,请确保为自己的CPT名称更改“custom\\u post\\u type”。出现的情况很多,“查找/替换”是一种简单的方法,可以捕获所有这些情况。所有这些代码都可以放入您的函数中。php或插件
Step 1: 注册帖子时,通过将“重写”设置为“false”,禁用自定义帖子类型的重写:
register_post_type( \'custom_post_type\',
array(
\'rewrite\' => false
)
);
Step 2: 手动将自定义重写添加到WordPress重写的底部
function custom_post_type_rewrites() {
add_rewrite_rule( \'[^/]+/attachment/([^/]+)/?$\', \'index.php?attachment=$matches[1]\', \'bottom\');
add_rewrite_rule( \'[^/]+/attachment/([^/]+)/trackback/?$\', \'index.php?attachment=$matches[1]&tb=1\', \'bottom\');
add_rewrite_rule( \'[^/]+/attachment/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$\', \'index.php?attachment=$matches[1]&feed=$matches[2]\', \'bottom\');
add_rewrite_rule( \'[^/]+/attachment/([^/]+)/(feed|rdf|rss|rss2|atom)/?$\', \'index.php?attachment=$matches[1]&feed=$matches[2]\', \'bottom\');
add_rewrite_rule( \'[^/]+/attachment/([^/]+)/comment-page-([0-9]{1,})/?$\', \'index.php?attachment=$matches[1]&cpage=$matches[2]\', \'bottom\');
add_rewrite_rule( \'[^/]+/attachment/([^/]+)/embed/?$\', \'index.php?attachment=$matches[1]&embed=true\', \'bottom\');
add_rewrite_rule( \'([^/]+)/embed/?$\', \'index.php?custom_post_type=$matches[1]&embed=true\', \'bottom\');
add_rewrite_rule( \'([^/]+)/trackback/?$\', \'index.php?custom_post_type=$matches[1]&tb=1\', \'bottom\');
add_rewrite_rule( \'([^/]+)/page/?([0-9]{1,})/?$\', \'index.php?custom_post_type=$matches[1]&paged=$matches[2]\', \'bottom\');
add_rewrite_rule( \'([^/]+)/comment-page-([0-9]{1,})/?$\', \'index.php?custom_post_type=$matches[1]&cpage=$matches[2]\', \'bottom\');
add_rewrite_rule( \'([^/]+)(?:/([0-9]+))?/?$\', \'index.php?custom_post_type=$matches[1]\', \'bottom\');
add_rewrite_rule( \'[^/]+/([^/]+)/?$\', \'index.php?attachment=$matches[1]\', \'bottom\');
add_rewrite_rule( \'[^/]+/([^/]+)/trackback/?$\', \'index.php?attachment=$matches[1]&tb=1\', \'bottom\');
add_rewrite_rule( \'[^/]+/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$\', \'index.php?attachment=$matches[1]&feed=$matches[2]\', \'bottom\');
add_rewrite_rule( \'[^/]+/([^/]+)/(feed|rdf|rss|rss2|atom)/?$\', \'index.php?attachment=$matches[1]&feed=$matches[2]\', \'bottom\');
add_rewrite_rule( \'[^/]+/([^/]+)/comment-page-([0-9]{1,})/?$\', \'index.php?attachment=$matches[1]&cpage=$matches[2]\', \'bottom\');
add_rewrite_rule( \'[^/]+/([^/]+)/embed/?$\', \'index.php?attachment=$matches[1]&embed=true\', \'bottom\');
}
add_action( \'init\', \'custom_post_type_rewrites\' );
注意:根据需要,您可能需要修改上述重写(禁用trackback?feeds?等)。这些表示如果没有在步骤1中禁用重写,将生成的“默认”重写类型
Step 3: 再次将永久链接到自定义帖子类型“pretty”
function custom_post_type_permalinks( $post_link, $post, $leavename ) {
if ( isset( $post->post_type ) && \'custom_post_type\' == $post->post_type ) {
$post_link = home_url( $post->post_name );
}
return $post_link;
}
add_filter( \'post_type_link\', \'custom_post_type_permalinks\', 10, 3 );
注意:如果您不担心您的用户在另一种帖子类型中创建冲突(重复)的帖子,那么您可以到此为止,这将导致在请求页面时,只有其中一人可以加载
Step 4: 防止重复post段塞
function prevent_slug_duplicates( $slug, $post_ID, $post_status, $post_type, $post_parent, $original_slug ) {
$check_post_types = array(
\'post\',
\'page\',
\'custom_post_type\'
);
if ( ! in_array( $post_type, $check_post_types ) ) {
return $slug;
}
if ( \'custom_post_type\' == $post_type ) {
// Saving a custom_post_type post, check for duplicates in POST or PAGE post types
$post_match = get_page_by_path( $slug, \'OBJECT\', \'post\' );
$page_match = get_page_by_path( $slug, \'OBJECT\', \'page\' );
if ( $post_match || $page_match ) {
$slug .= \'-duplicate\';
}
} else {
// Saving a POST or PAGE, check for duplicates in custom_post_type post type
$custom_post_type_match = get_page_by_path( $slug, \'OBJECT\', \'custom_post_type\' );
if ( $custom_post_type_match ) {
$slug .= \'-duplicate\';
}
}
return $slug;
}
add_filter( \'wp_unique_post_slug\', \'prevent_slug_duplicates\', 10, 6 );
注意:这将在任何重复段塞的末尾附加字符串“-duplicate”。如果在实现此解决方案之前已经存在重复的段塞,则此代码无法防止重复的段塞。确保首先检查重复项
我很想听听其他人的反馈,看看这对他们是否也有效。