Customize category URL

时间:2013-12-27 作者:blg002

我到处搜索,没有找到我要找的答案。默认情况下,我有:

示例。com/blog(博客存档)示例。com/blog/entry(单个条目)示例。com/blog/category/announcements(单一分类视图)现在,我创建了一个自定义的帖子类型&;分类法(类别),但我很难镜像博客的URL结构。例如,我的目标是:

示例。com/talks(talks归档)示例。com/talks/entry(单键通话输入)示例。com/talks/category/support(单一分类视图)以下是我目前掌握的内容:

function uc_add_talks_post_type() {
  $talks_labels = array(
    \'name\'                => \'Talks\',
    \'singular_name\'       => \'Talk\',
    \'all_items\'           => \'All Talks\',
     …
  );

  $talks_args = array(
    \'labels\'             => $talks_labels,
    \'public\'             => true,
    \'publicly_queryable\' => true,
    \'show_ui\'            => true,
    \'menu_position\'      => 5,
    \'rewrite\'            => array( \'slug\' => \'talks\', \'with_front\' => false ),
    \'has_archive\'        => \'talks\',
  );

  $talks_cat_args = array(
    \'hierarchical\' => true,
    \'labels\' => array(
      \'name\' => \'Talk categories\',
      \'singular_name\' => \'Talk category\',
      …
    ),
    \'show_admin_column\' => true,
    \'show_ui\' => true,
    \'query_var\' => true,
    \'rewrite\' => array(
      \'slug\' => \'talks\',
      \'with_front\' => false
    ),
  );

  register_post_type( \'uc_talks\', $talks_args );
  register_taxonomy( \'uc_talks_cat\', array(\'uc_talks\'), $talks_cat_args );
}

add_action( \'init\', \'uc_add_talks_post_type\');

1 个回复
SO网友:blg002

我通过阅读了解到这一点this article. 这很简单,我所要做的就是:

更改$talks_cat_args[rewrite][slug] 从…起\'talks\'\'talks/category\' (我以前试过)但没成功…

  • 确保register_taxonomy 在之前register_post_type.
  • 读了这篇文章,但这基本上是因为“WP\\U重写的工作原理是,从某种程度上讲,破译永久链接的过程是自上而下的”

    为了子孙后代,这里是最后的代码(稍加修改)。我不能说这是最好或最干净的方法,因为我对WordPress&;很陌生;PHP,但到目前为止它仍在工作。

     $talks_labels = array(
        \'name\'                => \'Talks\',
        \'singular_name\'       => \'Talk\',
        \'all_items\'           => \'All Talks\',
        \'add_new\'             => \'Add new\',
        \'add_new_item\'        => \'Add new Talk\',
        \'edit_item\'           => \'Edit Talk\',
        \'new_item\'            => \'New Talk\',
        \'view_item\'           => \'View Talk\',
        \'search_items\'        => \'Search Talks\',
        \'not_found\'           => \'No Talks found\',
        \'not_found_in_trash\'  => \'No Talks found in Trash\',
      );
    
      $talks_args = array(
        \'labels\'             => $talks_labels,
        \'public\'             => true,
        \'publicly_queryable\' => true,
        \'show_ui\'            => true,
        \'menu_position\'      => 5,
        \'rewrite\'            => array( \'slug\' => \'talks\', \'with_front\' => false ),
        \'has_archive\'        => \'talks\',
      );
    
    
      $talks_cat_labels= array(
        \'name\'              => \'Talk categories\',
        \'singular_name\'     => \'Talk category\',
        \'search_items\'      => \'Search talk categories\',
        \'all_items\'         => \'All talk categories\',
        \'parent_item\'       => \'Parent talk category\',
        \'parent_item_colon\' => \'Parent talk category:\',
        \'edit_item\'         => \'Edit talk category\',
        \'update_item\'       => \'Update talk category\',
        \'add_new_item\'      => \'Add new talk category\',
        \'new_item_name\'     => \'New talk category name\',
      );
    
      $talks_cat_args = array(
        \'hierarchical\' => true,            
        \'labels\' => $talks_cat_labels,
        \'show_admin_column\' => true, 
        \'show_ui\' => true,
        \'query_var\' => true,
        \'rewrite\' => array(
          \'slug\' => \'talks/category\',
          \'with_front\' => false
        ),
      );
    
      register_taxonomy( \'uc_talks_cat\', array(\'uc_talks\'), $talks_cat_args );
      register_post_type( \'uc_talks\', $talks_args );
    

    结束