自定义分类和POST辅助程序永久链接

时间:2015-08-23 作者:Atticus

我有一个自定义分类法和一个自定义帖子类型。我的目标是实现以下permalink结构:

{custom taxonomy}/{custom taxonomy term}/{custom post slug}

或者,至少,

{custom taxonomy term}/{custom post slug}

我目前有

{%category%}/{%postname%}, 但没有骰子。

例如,假设我有:

分类法states, 和一个分类术语newyorkjobs 用一根有子弹的柱子wordpress-developer

  • states/newyork: 所有jobs 在里面newyork, 这是可行的
  • states/newyork/wordpress-developer: 使用分类法的自定义帖子页面;术语作为永久链接中的基states/newyork (到目前为止,没有偏离permalink结构的“工作”列表和流入个人岗位states/newyork/wordpress-developer

    遇到的问题:

    • states/newyork/wordpress 产生a404
    • newyork/wordpress-developer 重定向到jobs/wordpress-developer

      • states/newyork/wordpress-developer 登陆自定义帖子类型页面
      PS-不构建另一个工作板,仅以此为例:)

      当前分类法;立柱式寄存器挂钩:

      function register() {
        register_taxonomy(\'state\', \'district\', array(
          \'labels\' => array(
            \'name\' => \'States\',
            \'singular_name\' => \'State\',
            \'search_items\' => \'Search States\',
            \'all_items\' => \'All States\',
            \'parent_item\' => \'Parent State\',
            \'parent_item_colon\' => \'Parent State:\',
            \'edit_item\' => \'Edit State\',
            \'update_item\' => \'Update State\',
            \'add_new_item\' => \'Add New State\',
            \'new_item_name\' => \'New State\',
            \'menu_name\' => \'States\'
          ),
          \'public\' => true,
          \'show_admin_column\' => true,
          \'hierarchical\' => true,
          \'query_var\' => true,
          \'rewrite\' => array(
            \'slug\' => \'states\',
            \'with_front\' => false
          )
        ));
      
        register_post_type(\'district\',
          array(
            \'labels\' => array(
              \'name\' => \'Districts\',
              \'singular_name\' => \'Districts\'
            ),
            \'hierarchical\' => true,
            \'supports\' => array(\'title\', \'page-attributes\'),
            \'public\' => true,
            \'has_archive\' => \'states\',
            \'rewrite\' => array(
              \'with_front\' => false,
              \'slug\' => \'states/%show_category%\'
            )
          )
        );
      }
      

1 个回复
SO网友:Atticus

我可以通过定义重写规则来解决这个问题:

function rewrite_rules($rules) {
  $newRules = array();
  $newRules[\'states/(.+)/(.+?)$\'] = \'index.php?district=$matches[2]\';
  return array_merge($newRules, $rules);
}
add_filter(\'rewrite_rules_array\', __NAMESPACE__ . \'\\\\rewrite_rules\');

结束

相关推荐

Change Taxonomy Permalinks

我有自定义帖子,我创建了一个显示所有自定义帖子的页面。示例:www.example.com/archive-page我想知道是否可以更改与此自定义帖子相关的类别和标签的永久链接。现在我有:www.example.com/my-custom-post-type-cats/my-category-1www.example.com/my-custom-post-type-tags/my-tag-1</我想要这样的东西:www.example.com/archive-page?category=1www.e