类别存档的自定义固定链接(自定义基础和无父插件)

时间:2018-09-08 作者:terminator

对于默认的分类法(即类别),我希望使用

i) base = "post/c"  and 
ii) without any parent slug
例如:如果我有类别

cat-a
   subcat-a1
   subcat-a2
cat-b
   subcat-a2
   subcat-a2
所以我想要像

实例com/post/c/cat示例。com/post/c/subcat-a1示例。com/post/c/subcat-a2示例。以com/post/c/cat为例。com/post/c/subcat-b1示例。com/post/c/subcat-b2

我正在使用以下代码。。I解决了问题(ii),但没有解决问题(I),即基本“后/c”

add_filter(\'term_link\', \'ojasya_no_term_parents\', 1000, 3);
function ojasya_no_term_parents($url, $term, $taxonomy) {
if($taxonomy == \'category\'){
$term_nicename = $term->slug;
$url = trailingslashit(get_option( \'home\' )) . user_trailingslashit( $term_nicename, \'category\' );
}
return $url;
}
// Add our custom post cat rewrite rules
add_filter(\'rewrite_rules_array\', \'ojasya_no_category_parents_rewrite_rules\');
function ojasya_no_category_parents_rewrite_rules($rules) {
$new_rules = array();
$terms = get_terms( array(
\'taxonomy\' => \'category\',
\'post_type\' => \'post\',
\'hide_empty\' => false,
));
if($terms && !is_wp_error($terms)){
foreach ($terms as $term){
$term_slug = $term->slug;
$new_rules[$term_slug.\'/?$\'] = \'index.php?category=\'.$term_slug;
$new_rules[$term_slug.\'/page/([0-9]{1,})/?$\'] = \'index.php?category=\'.$term_slug.\'&paged=$matches[1]\';
$new_rules[$term_slug.\'/(?:feed/)?(feed|rdf|rss|rss2|atom)/?$\'] = \'index.php?category=\'.$term_slug.\'&feed=$matches[1]\';
}
}
return $new_rules + $rules;
}
请帮我解决这个问题。提前感谢

2 个回复
SO网友:noahmason

要实现这一点,您所要做的就是修改category 使用分类参数register_taxonomy() 再次在initcategory 分类法已注册。这可以通过以下代码段中的任何分类法来完成。

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分类法categorypost_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 两个都为falsecategorypost_tag. 然后将永久链接设置为Settings->Permalinks 如下图所示;)

Cat Tag 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 在两种情况下均为truecategorypost_tag. 然后,将永久链接设置为Settings->Permalinks 如下图所示,请记住,您放置在第一个重写标记之前的任何纯文本标记(%...%) 后PERMALINK设置将显示在cat/tag PERMALINK base之前。If this is set to /post/%...%/%...%/%...%/, this is the best option, for sure.

Permalink Settings Screen Shot

SO网友:David Corp

如果您将在url中的任何位置添加帖子id,那么这将很容易,否则您的帖子标题必须是唯一的。因此,通过标题和使用获取整个帖子add_rewrite_rule 作用

结束