rewrite rules hierarchical

时间:2012-07-16 作者:jdivins

我希望我的WordPress遵循以下规则:

  1. /productes/ 包含所有产品的页面/productes/[category]/ 包含该类别所有产品的分类页面/productes/[category]/[subcategory]/ 包含该类别所有产品(与2相同)的分类页面/[productes]/[category]/[product]/ A.single-productes.php 将显示类别产品/[productes]/[category]/[subcategory]/[product]/ A.single-productes.php 将显示子类别产品(与4相同)
[category]/[subcategory] 层次分类法是否等效于%categories_productes%

我有两条规则:

add_filter(\'rewrite_rules_array\', \'tatanet_rewrite_rules\');
function tatanet_rewrite_rules($rules) {
  $newRules  = array();
  $newRules[\'(.+)/?$\']                                  = \'index.php?pagename=$matches[1]\';
  $newRules[\'productes/(.+?)/([^/]+)(/[0-9]+)?/?$\']     = \'index.php?productes=$matches[3]\';
  return array_merge($newRules, $rules);
}
我已注册此分类法:

$labels = array(
\'name\'                          => \'Categories\',
\'singular_name\'                 => \'Categoria\',
\'search_items\'                  => \'Buscar Categories\',
\'all_items\'                     => \'Totes les Categories\',
\'parent_item\'                   => \'Categoria superior\',
\'edit_item\'                     => \'Editar Categoria\',
\'update_item\'                   => \'Actualitzar Categoria\',
\'add_new_item\'                  => \'Afegir nova Categoria\',
\'new_item_name\'                 => \'Nova Categoria\',
\'separate_items_with_commas\'    => \'Separar Categories amb comes\',
\'add_or_remove_items\'           => \'Afegir o treure Categoria\',
);
$args = array(
\'label\'                         => \'Categories\',
\'labels\'                        => $labels,
\'public\'                        => true,
\'hierarchical\'                  => true,
\'show_ui\'                       => true,
\'show_in_nav_menus\'             => true,
\'query_var\'                     => true,
\'args\'                          => array( \'orderby\' => \'term_order\' ),
\'rewrite\'                       => array( \'slug\' => \'productes\', \'with_front\' => false, \'hierarchical\' => true )
);
register_taxonomy( \'categories_productes\', \'productes\', $args );
我已注册此帖子类型:

$labels = array(
  \'name\' => _x(\'Productes\', \'post type general name\'),
  \'singular_name\' => _x(\'Producte\', \'post type singular name\'),
  \'add_new\' => _x(\'Afegir nou\', \'productes\'),
  \'add_new_item\' => __(\'Afegir nou producte\'),
  \'edit_item\' => __(\'Editar producte\'),
  \'new_item\' => __(\'Nou producte\'),
  \'all_items\' => __(\'Tots els productes\'),
  \'view_item\' => __(\'Veure producte\'),
  \'search_items\' => __(\'Cercar productes\'),
  \'not_found\' =>  __(\'No s\\\'han trobat productes\'),
  \'not_found_in_trash\' => __(\'No s\\\'han trobat productes a la Paperera\'), 
  \'parent_item_colon\' => \'\',
  \'menu_name\' => \'Productes\'
);
$args = array(
  \'labels\' => $labels,
  \'public\' => true,
  \'publicly_queryable\' => true,
  \'show_ui\' => true, 
  \'show_in_menu\' => true, 
  \'query_var\' => true,
  \'capability_type\' => \'post\',
  \'has_archive\' => \'productes\', 
  \'hierarchical\' => false,
  \'menu_position\' => -4,
  \'rewrite\' => array( \'slug\' => \'productes/%categories_productes%\', \'with_front\' => false, \'hierarchical\' => true ),
  \'supports\' => array( \'title\' )
); 
register_post_type( \'productes\', $args );
}
这是URL和页面加载结果:

  1. /productes/ 确定=>加载运行的页面产品template-productes.php
  2. /productes/[category]/ 正常=>负载taxonomy-categories_productes.php
  3. /productes/[category]/[subcategory]/ ?? => 404未找到错误/[productes]/[category]/[product]/ 正常=>负载single-productes.php[product]
  4. /[productes]/[category]/[subcategory]/[product]/ 正常=>负载single-productes.php[product]

    我有一个名为Products的页面(可能是它导致了冲突?)

    我有一个permalink的帖子重写器:

    add_filter(\'post_link\', \'categories_productes_permalink\', 10, 3);
    add_filter(\'post_type_link\', \'categories_productes_permalink\', 10, 3);
    
    function categories_productes_permalink($permalink, $post_id, $leavename) {
      if (strpos($permalink, \'%categories_productes%\') === FALSE) return $permalink;
      $post = get_post($post_id);
      if (!$post) return $permalink;
      // get taxonomy terms
      $terms = wp_get_object_terms($post->ID, \'categories_productes\');  
      if (!is_wp_error($terms) && !empty($terms) && is_object($terms[0])) $taxonomy_slug = $terms[0]->slug;
      else $taxonomy_slug = \'uncategorized\';
      return str_replace(\'%categories_productes%\', $taxonomy_slug, $permalink);
    }
    

1 个回复
SO网友:Milo

当Wordpress解析传入的请求时,它无法区分什么是产品,什么是子类别。一个优先-它将始终查找匹配的子类别或始终查找匹配的产品,这取决于它处理规则的顺序。

结束

相关推荐