generate\\u rewrite\\u规则对我根本不起作用,我在论坛上发现了其他人有相同问题的帖子。添加到rewrite\\u rules\\u数组确实有效。下面是解决方案。
add_filter( \'post_link\', \'custom_permalink\', 10, 3 );
add_filter(\'rewrite_rules_array\',\'wp_insertMyRewriteRules\');
add_filter(\'init\',\'flushRules\');
function custom_permalink( $permalink, $post, $leavename ) {
$category = get_the_category($post->ID);
if ( !empty($category) && $category[0]->cat_name == "Shop" )
{
$permalink = trailingslashit( home_url(\'shop/\' . $post->post_name ) );
}
return $permalink;
}
function flushRules(){
global $wp_rewrite;
$wp_rewrite->flush_rules();
}
function wp_insertMyRewriteRules($rules)
{
$newrules = array();
$newrules[\'^shop/(.*)$\'] = \'index.php?name=$matches[1]\';
return $newrules + $rules;
}