我只是希望将两个自定义帖子类型元属性作为URL的标识符。我实现了一个解决方案,但它只有404个
注册帖子类型:
final private static function register_post_types ()
{
$labels = array(
'name' => _x( 'Properties', 'post type general name', ' propertystreambootstrap' ),
'singular_name' => _x( 'Property', 'post type singular name', ' propertystreambootstrap' ),
'menu_name' => _x( 'Properties', 'admin menu', ' propertystreambootstrap' ),
'name_admin_bar' => _x( 'Property', 'add new on admin bar', ' propertystreambootstrap' ),
'add_new' => _x( 'Add New', 'property', ' propertystreambootstrap' ),
'add_new_item' => __( 'Add New Property', ' propertystreambootstrap' ),
'new_item' => __( 'New Property', ' propertystreambootstrap' ),
'edit_item' => __( 'Edit Property', ' propertystreambootstrap' ),
'view_item' => __( 'View Property', ' propertystreambootstrap' ),
'all_items' => __( 'All Properties', ' propertystreambootstrap' ),
'search_items' => __( 'Search Properties', ' propertystreambootstrap' ),
'parent_item_colon' => __( 'Parent Properties:', ' propertystreambootstrap' ),
'not_found' => __( 'No properties found.', ' propertystreambootstrap' ),
'not_found_in_trash' => __( 'No properties found in Trash.', ' propertystreambootstrap' )
);
$args = array(
'labels' => $labels,
'description' => __( 'Properties for your website.', ' propertystreambootstrap' ),
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'menu_icon' => 'dashicons-building',
'query_var' => true,
//'rewrite' => array( 'slug' => 'malta-property/%department%/%location%/%reference%' ),
'rewrite' => array( 'slug' => 'malta-property/%location%/%reference%'),
'has_archive' => 'about-cool-post-types',
'capability_type' => 'post',
'has_archive' => false,
'hierarchical' => false,
'menu_position' => null,
'supports' => array('title', 'editor', 'excerpt', 'post-thumbnails', 'thumbnail')
);
register_post_type( 'property', $args );
}
网址重写:
final public static function url_rewrite()
{
global $wp_rewrite;
$wp_rewrite->add_rewrite_tag('%reference%', '([^&]+)', 'reference=');
$wp_rewrite->add_rewrite_tag('%location%', '([^&]+)', 'location=');
return null;
}
过滤器功能
final public static function permalink_structure ($permalink, $post, $leavename)
{
if (false !== strpos($permalink, '%reference%')) {
$reference = get_post_meta($post->ID, 'propertystream_agent_ref', true);
$location = wp_get_post_terms($post->ID, 'location');
$location = str_replace('-cat', '', $location[0]->slug);
// die($location);
// $department = wp_get_post_terms($post->ID, 'department');
$rewritecode = array(
'%reference%',
'%location%',
// '%department%',
$post->post_name,
);
$rewritereplace = array(
$reference,
$location,
// $department,
$leavename? $post->post_name : '',
);
$permalink = str_replace($rewritecode, $rewritereplace, $permalink);
}
return $permalink;
}
这一切都生成了我想要的代码,但它只是404的代码-要使其正常工作,我必须在过滤器函数中添加$ leavename = true,但这会将后缀添加到URL中,我不希望这样做。