自定义帖子类型将url重写到/ategory/pagetitle

时间:2016-04-08 作者:Jeff

我知道这个问题被问了很多,但在连续6个小时的研究之后,我在任何地方都找不到好的解决方案。

我的网站有一个自定义的帖子类型和分类法。我的分类法是惠普、戴尔和华硕。每种产品都检查了它们的分类。现在我需要重写我的url,给我一个不同的链接(即products/$taxonomy/$productname), 所以结果应该是products/asus/n56jn. Asus是分类法,n56jn是产品名称。

Functions.php

Register Post Type

function site_add_cpt() {
  register_post_type( \'cpt_products\', array(
  \'labels\'            =>  array(
    \'name\'          =>      __( \'Products\' ),
    \'singular_name\' =>      __( \'Product\' ),
    \'all_items\'     =>      __( \'View Products\' ),
    \'add_new\'       =>      __( \'New Product\' ),
    \'add_new_item\'  =>      __( \'New Product\' ),
    \'edit_item\'     =>      __( \'Edit Product\' ),
    \'view_item\'     =>      __( \'View Product\' ),
    \'search_items\'  =>      __( \'Search Products\' ),
    \'no_found\'      =>      __( \'No Products Found\' ),
    \'not_found_in_trash\' => __( \'No Products in Trash\' )
    ),
    \'public\'            =>  true,
    \'publicly_queryable\'=>  true,
    \'show_ui\'           =>  true,
    \'show_in_nav_menus\' =>  true,
    \'capability_type\'   =>  \'page\',
    \'hierarchical\'      =>  true,
    \'rewrite\'           =>  array( \'slug\' => \'%product_item%\', \'with_front\' => false ),
    \'menu_icon\'         =>  \'dashicons-cart\',
    \'menu_position\'     =>  21,
    \'supports\'          =>  array( \'title\', \'editor\', \'page-attributes\', \'revisions\' ),
) );

  );
}
add_action(\'init\', \'site_add_cpt\');

Register Taxonomy

function site_add_tax() {
  register_taxonomy(
    \'tax_products\',
    \'cpt_products\',
    array(
      \'labels\'            =>  array(
      \'name\'              => __( \'Product Categories\' ),
      \'singular_name\'     => __( \'Product Category\' ),
      \'search_items\'      => __( \'Search Product Categories\' ),
      \'all_items\'         => __( \'All Product Categories\' ),
      \'parent_item\'       => __( \'Parent Product Category\' ),
      \'parent_item_colon\' => __( \'Parent Product Category:\' ),
      \'edit_item\'         => __( \'Edit Product Category\' ),
      \'update_item\'       => __( \'Update Product Category\' ),
      \'add_new_item\'      => __( \'Add New Product Category\' ),
      \'new_item_name\'     => __( \'New Product Category\' ),
      \'menu_name\'         => __( \'Product Categories\' )
      ),
      \'public\'            =>  true,
      \'hierarchical\'      =>  true,
      \'show_in_nav_menus\' =>  true,
      \'show_admin_column\' =>  true,
      \'rewrite\'           =>  array( \'slug\' => \'products\', \'with_front\' => false, \'hierarchical\' => true )
    ));
}
add_action(\'init\', \'site_add_taxt\');

1 个回复
SO网友:Jeff

正如米洛在评论中所说(非常感谢!)答案已发布:Permalinks: custom post type -> custom taxonomy -> post

相关推荐