要实现这一点,您所要做的就是修改category
使用分类参数register_taxonomy()
再次在init
在category
分类法已注册。这可以通过以下代码段中的任何分类法来完成。
Code for Taxonomies in General (Code for this use case below):
function mytheme_change_category_args() {
// Get the taxonomy.
$category = get_taxonomy( \'category\' );
// change the rewrite slug on the taxonomy itself.
// Note that if your permalink structure is currently
// "/post/%postname%", this should be changed to "c", and
// "with_front" should be set to "true".
$category->rewrite[\'slug\'] = \'post/c\';
// do not use the text that comes before the first
// rewrite tag (%...%) in the permalink settings, unless the
// setting is "/post/%postname%" as explained above.
$category->rewrite[\'with_front\'] = false;
// Keep the category hierarchical, but make the permalinks flat.
$category->rewrite[\'hierarchical\'] = false;
// register_taxonomy() is used to create OR modify a taxonomy.
// make sure the taxonomy object is cast to an array.
register_taxonomy( \'category\', \'post\', (array) $category );
}
// Make sure this happens after the original tax is init. Priority 10 is fine.
add_action( \'init\', \'mytheme_change_category_args\', 10 );
Note:
转到
Settings->Permalinks
并点击“保存更改”刷新重写规则。无需过滤术语链接、添加重写规则或其他任何操作。
<小时>
To answer your question specifically:
为了做到这一点,核心WP分类法
category
和
post_tag
, 我们必须记住以下位置的类别和标记永久链接设置:
Settings->Permalinks
上面的例子没有。最好的方法其实也是最简单的方法。
Code for this use case:
一
function mytheme_change_category_args() {
// Get the taxonomy.
$category = get_taxonomy( \'category\' );
// change the rewrite slug on the taxonomy itself.
// Note that if your permalink structure is currently
// "/post/%postname%", this should be changed to "c", and
// "with_front" should be set to "true".
$category->rewrite[\'slug\'] = \'post/c\';
// do not use the text that comes before the first
// rewrite tag (%...%) in the permalink settings, unless the
// setting is "/post/%postname%" as explained above.
$category->rewrite[\'with_front\'] = false;
// Keep the category hierarchical, but make the permalinks flat.
$category->rewrite[\'hierarchical\'] = false;
// register_taxonomy() is used to create OR modify a taxonomy.
// make sure the taxonomy object is cast to an array.
register_taxonomy( \'category\', \'post\', (array) $category );
}
// Make sure this happens after the original tax is init. Priority 10 is fine.
add_action( \'init\', \'mytheme_change_category_args\', 10 );
你所要做的就是category
到非层次永久链接,并设置with_front
两个都为falsecategory
和post_tag
. 然后将永久链接设置为Settings->Permalinks
如下图所示;)
Option 2:
function mytheme_change_category_args() {
$category = get_taxonomy( \'category\' );
$post_tag = get_taxonomy( \'post_tag\' );
$category->rewrite[\'hierarchical\'] = false;
$category->rewrite[\'with_front\'] = true;
$post_tag->rewrite[\'with_front\'] = true;
register_taxonomy( \'category\', \'post\', (array) $category );
register_taxonomy( \'post_tag\', \'post\', (array) $post_tag );
}
add_action( \'init\', \'mytheme_change_category_args\', 50 );
在这种情况下,您需要做的是设置category
到非层次永久链接(如选项1所示),并设置with_front
在两种情况下均为truecategory
和post_tag
. 然后,将永久链接设置为Settings->Permalinks
如下图所示,请记住,您放置在第一个重写标记之前的任何纯文本标记(%...%
) 后PERMALINK设置将显示在cat/tag PERMALINK base之前。If this is set to /post/%...%/%...%/%...%/
, this is the best option, for sure.