有一种自定义的“房地产”类型,带有“卖”,有两个分类城市和房地产类型。必须显示税收是指相同的段塞,即
site.com/sale/tax-1/tax-2 - (site.com/sale/california/apartment) - works
site.com/sale/tax-1 - (site.com/sale/california) - works
site.com/sale/tax-2 - (site.com/sale/apartment) - does not work, 404 error, because it expects a taxonomy term for the "city", and since there is no such city as "apartment", it means that it gets an error. (apartment - this is a taxonomy term for a property type)我明白为什么它不是那样工作的,但这就是它所需要的。因为您需要通过"city“和属性类型同时和单独地显示对象,也就是说,您需要
site.com/sale/california/apartment - thus [post type > tax - city > tax - property type]
site.com/sale/california - thus [post type > tax - city]
site.com/sale/apartment - thus [post type > tax - property type]
function register_realty_post_type() {
$args = array(
'labels' => array(
'name' => 'Ads',
),
'query_var' => true,
'rewrite' => array(
'slug' => 'sale/%realty_city%/%realty_type%',
'with_front' => true
),
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'has_archive' => 'sale',
'menu_position' => 15,
'supports' => array( 'title', 'editor', 'thumbnail', 'excerpt' ),
);
flush_rewrite_rules();
register_post_type('realty', $args);
}
add_action( 'init', 'register_realty_post_type' );
function register_realty_taxonomies() {
$taxonomies = array();
$taxonomies['realty_type'] = array(
'hierarchical' => true,
'rewrite' => array(
'slug' => 'sale'
),
'labels' => array(
'name' => 'Type realty',
),
'show_admin_column' => true
);
$taxonomies['realty_city'] = array(
'hierarchical' => true,
'rewrite' => array(
'slug' => 'sale'
),
'labels' => array(
'name' => 'City',
),
'show_admin_column' => true
);
foreach( $taxonomies as $name => $args ) {
register_taxonomy( $name, array( 'realty' ), $args );
}
}
add_action( 'init', 'register_realty_taxonomies' );
function filter_post_type_link_realty_type($link, $post) {
if( $post->post_type != 'realty' ) {
return $link;
}
if( $cats = get_the_terms($post->ID, 'realty_type') ) {
$link = str_replace('%realty_type%', array_pop($cats)->slug, $link);
return $link;
}
}
add_filter('post_type_link', 'filter_post_type_link_realty_type', 10, 2);
function filter_post_type_link_realty_city($link, $post) {
if( $post->post_type != 'realty' ) {
return $link;
}
if( $cats = get_the_terms($post->ID, 'realty_city') ) {
$link = str_replace('%realty_city%', array_pop($cats)->slug, $link);
return $link;
}
}
add_filter('post_type_link', 'filter_post_type_link_realty_city', 10, 2);发布于 2023-05-16 15:55:45
通过过滤器请求解决了这个问题。
由于我们有一个问题,即2赋税指的是1和相同的段塞,所以我通过过滤器更改查询参数。
网站/销售/巴塞罗那- works网站/销售/别墅- 404错误,bcz WP期望它是一个城市分类术语,而不是一个属性类型。
通过筛选器,我检查属性类型是否有这样的术语,如果有,我将按city 删除分类法请求,并按属性类型<#>添加它。正在发生变化。
我这样做的城市分类法和属性类型<#>
// For realty_type
add_filter('request', 'request_filter_for_realty_type');
function request_filter_for_realty_type($query_vars) {
if
( isset($query_vars['realty_city'])
&& !get_term_by('slug', $query_vars['realty_city'], 'realty_city')
&& get_term_by('slug', $query_vars['realty_city'], 'realty_type')
)
{
$term = $query_vars['realty_city'];
unset($query_vars['realty_city']);
$query_vars['realty_type'] = $term;
}
return $query_vars;
}
// For realty_city
add_filter('request', 'request_filter_for_realty_city');
function request_filter_for_realty_city($query_vars) {
if
( isset($query_vars['realty_type'])
&& !get_term_by('slug', $query_vars['realty_type'], 'realty_type')
&& get_term_by('slug', $query_vars['realty_type'], 'realty_city')
)
{
$term = $query_vars['realty_type'];
unset($query_vars['realty_type']);
$query_vars['realty_city'] = $term;
}
return $query_vars;
}发布于 2023-05-16 05:58:26
WordPress是防止两个相同的鼻涕虫到两个不同的内容。如果URL可能是相等的,他怎么知道把你送到哪里去呢?
我认为你需要以不同的方式来设计你的开发。有一个不分层次的术语,然后进行URL重写。你只能有一个“减价”,并将它添加到“加利福尼亚”或“公寓”的帖子中。
https://wordpress.stackexchange.com/questions/416050
复制相似问题