首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >两个具有相同鼻涕虫的分类

两个具有相同鼻涕虫的分类
EN

WordPress Development用户
提问于 2023-05-15 14:11:11
回答 2查看 40关注 0票数 1

有一种自定义的“房地产”类型,带有“卖”,有两个分类城市和房地产类型。必须显示税收是指相同的段塞,即

代码语言:javascript
复制
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“和属性类型同时和单独地显示对象,也就是说,您需要

代码语言:javascript
复制
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);
EN

回答 2

WordPress Development用户

回答已采纳

发布于 2023-05-16 15:55:45

通过过滤器请求解决了这个问题。

由于我们有一个问题,即2赋税指的是1和相同的段塞,所以我通过过滤器更改查询参数。

网站/销售/巴塞罗那- works网站/销售/别墅- 404错误,bcz WP期望它是一个城市分类术语,而不是一个属性类型。

通过筛选器,我检查属性类型是否有这样的术语,如果有,我将按city 删除分类法请求,并按属性类型<#>添加它。正在发生变化。

我这样做的城市分类法和属性类型<#>

代码语言:javascript
复制
// 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;
}
票数 1
EN

WordPress Development用户

发布于 2023-05-16 05:58:26

WordPress是防止两个相同的鼻涕虫到两个不同的内容。如果URL可能是相等的,他怎么知道把你送到哪里去呢?

我认为你需要以不同的方式来设计你的开发。有一个不分层次的术语,然后进行URL重写。你只能有一个“减价”,并将它添加到“加利福尼亚”或“公寓”的帖子中。

票数 1
EN
页面原文内容由WordPress Development提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://wordpress.stackexchange.com/questions/416050

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档