重写自定义POSTYPE和自定义分类的固定链接

时间:2010-12-17 作者:Henk Jan

如何为自定义posttype和自定义分类重写permalinks?

我有一个自定义分类法:region,和一个自定义posttype:business。

现在,如何重写永久链接

对于一个类别,固定的;“类别”;零件替换为自定义分类术语:

/%region%/%category%/

/%region%/%category%/%business%/

我被这件事难住了。然而,我确实成功地将某个区域的永久链接重写为/%region%/:

add_action( \'init\', \'region_init\' );
function region_init() {
  // set labels
    $labels = array(
    \'name\' => _x( \'Regions\', \'taxonomy general name\' ),
    \'singular_name\' => _x( \'Region\', \'taxonomy singular name\' ),
    \'search_items\' =>  __( \'Search Regions\' ),
    \'popular_items\' =>  __( \'Popular Regions\' ),
    \'all_items\' => __( \'All Regions\' ),
    \'parent_item\' => __( \'Parent Region\' ),
    \'parent_item_colon\' => __( \'Parent Region:\' ),
    \'edit_item\' => __( \'Edit Region\' ), 
    \'update_item\' => __( \'Update Region\' ),
    \'add_new_item\' => __( \'Add New Region\' ),
    \'new_item_name\' => __( \'New Region Name\' )
  );
  // create a new taxonomy
  register_taxonomy(
    \'region\',
    \'business\',
    array(
      \'labels\' => $labels,
      \'label\' => __(\'Region\'),
      \'sort\' => true,
      \'args\' => array(\'orderby\' => \'term_order\'),
      \'show_in_nav_menus\' => true,
      \'query_var\' => true,
      \'rewrite\' => array( \'slug\' => \'\', \'with_front\' => false )
    )
  );
}

add_action(\'init\', \'my_rewrite\');
function my_rewrite() {
    global $wp_rewrite;
    $wp_rewrite->add_permastruct(\'typename\', \'typename/%year%/%postname%/\', true, 1);
    add_rewrite_rule(\'typename/([0-9]{4})/(.+)/?$\', \'index.php?typename=$matches[2]\', \'top\');
    $wp_rewrite->flush_rules(); // !!!
}

1 个回复
最合适的回答,由SO网友:Henk Jan 整理而成

这个Custom Post Permalinks (Wordpress Plugin) 约翰·P·布洛赫给了我这个把戏。很棒/灵活的插件!

结束

相关推荐