自定义永久链接结构,用于某些类别的帖子

时间:2018-08-30 12:38:39

标签: wordpress hook permalinks

正如标题所述,我想为属于某个类别的帖子创建一个不同的永久链接结构,在我的情况下,名称为“ ICO”。我已经在StackExchange上找到了这段代码,但是与OP的反馈相反,它对我没有任何帮助。

代码是:

//Rewrite URLs for "ICO" category
add_filter( 'post_link', 'custom_permalink', 10, 3 );
function custom_permalink( $permalink, $post, $leavename ) {
// Get the category for the post
$category = get_the_category($post->ID);
if (  !empty($category) && $category[0]->cat_name == "ICO" ) {
    $cat_name = strtolower($category[0]->cat_name);
    $permalink = trailingslashit( home_url('/'. $cat_name . '/' . $post->post_name .'/' ) );
}
return $permalink;
}

add_action( 'init', 'custom_rewrite_rules' );
function custom_rewrite_rules() {
add_rewrite_rule(
    'ico/([^/]+)(?:/([0-9]+))?/?$',
    'index.php?category_name=ico&name=$matches[1]&page=$matches[2]',
    'top' // The rule position; either 'top' or 'bottom' (default).
);
}

首页上该类别的所有帖子均未更改其URL,默认URL结构与www.site.com/post_name /

相同

但是,我注意到在某些小部件上,具有该类别帖子的列表中,有些帖子的URL更改了,有些则没有。它们之间没有任何区别,它们具有相同的类别。即使我继续使用该URL / ico / post_name /,即使删除/ ico /并保留默认的永久链接结构,它也同样有效,从而使它成为同一帖子的重复URL。

我做错了什么?

0 个答案:

没有答案
相关问题