//那么,首先使用一个函数来提及您的自定义帖子类型。
function my_custom_post_types(){
return array(
\'os_estate\',
\'os_villas\',
\'os_chalets\',
\'os_cottages\'
);
}
//然后,过滤请求。
add_action( \'parse_request\',\'my_custom_post_types_parse_request\', 999);
function my_custom_post_types_parse_request( $query ){
if( isset($query->request) )
{
$req = explode(\'/\', ltrim($query->request,\'/\'));
if( !is_admin() && isset($req[\'0\']) )
{
global $wpdb;
$name = stripslashes($req[\'0\']);
$_post = $wpdb->get_row( "SELECT * FROM $wpdb->posts WHERE post_status = \'publish\' AND post_name =\'". $name ."\' AND post_type IN (\'" . join("\', \'", my_custom_post_types() ) . "\')" );
if( $_post )
{
$query->query_vars[\'error\'] = \'\';
$query->query_vars[\'post_type\'] = $_post->post_type;
$query->query_vars[\'name\'] = $name;
}
}
}
}
//然后过滤所有所需的帖子类型链接。
add_filter( \'post_type_link\', \'my_custom_post_types_permalink\', 10, 2);
function my_custom_post_types_permalink( $post_link, $post ){
if( in_array( $post->post_type, my_custom_post_types() ) && !empty($post->post_name) && $post->post_status == \'publish\' && \'\' != get_option(\'permalink_structure\') )
{
$post_name = $post->post_name;
$post_link = home_url("/$post_name/");
}
return $post_link;
}
编辑:修复错误,根据编辑6个字符的最低要求添加此行