在Wordpress中选择的永久链接结构为“ example.com/%postname%/”。类别为29的帖子应在https://example.com/dictionary/post-name/之前工作。请向我解释为什么此代码不起作用:
function dictionary_permalink($permalink, $post) {
$category = get_the_category($post->ID);
if (!empty($category) && $category[0]->cat_ID == 29) {
$permalink = trailingslashit( home_url('/dictionary/'. $post->post_name . '/'));
}
return $permalink;
}
add_filter('post_link', 'dictionary_permalink', 20, 3);
function dictionary_rewrite() {
add_rewrite_rule(
'^dictionary/([^/]*)/?',
'index.php?pagename=$matches[1]',
'top'
);
}
add_action('init', 'dictionary_rewrite');
页面“ example.com/dictionary/post-name/”显示404。当我输入“ example.com/index.php?pagename=post-name”时,页面将我重定向到“ example.com/post-name” /”(正确加载)