给你:
add_action(\'init\', \'wp_new_flush_rewrite_rules\'); //Run this action only ONCE if not you are looking at 2 extra db queries on every page load so dont forget to comment it out
function wp_new_flush_rewrite_rules() {
global $wp_rewrite;
$wp_rewrite->flush_rules();
}
add_action(\'generate_rewrite_rules\', \'new_rewrite_rules\');
function new_rewrite_rules( $wp_rewrite ) {
$post_type = \'post\';
$new_rules = array(
\'writer/(.+?)/page/?([0-9]{1,})/?$\' => \'index.php?post_type=\'.$post_type.\'&meta_key=writer&meta_value=\'.$wp_rewrite->preg_index(1).\'&paged=\'.$wp_rewrite->preg_index(2),
\'writer/(.+?)/?$\' => \'index.php?post_type=\'.$post_type.\'&meta_key=writer&meta_value=\'.$wp_rewrite->preg_index(1),
);
$wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
return $wp_rewrite;
}
add_filter(\'query_vars\', \'add_query_vars\');
function add_query_vars( $vars ){
$vars[] = \'meta_key\';
$vars[] = \'meta_value\';
return $vars;
}
add_action(\'template_redirect\', \'template_redirect_intercept\');
function template_redirect_intercept(){
global $wp_query;
if ( $wp_query->get(\'writer\') && file_exists( TEMPLATEPATH . \'/author.php\' ) ){
$wp_query->is_404 = \'\'; $wp_query->is_archive = 1; $wp_query->is_post_type_archive = 1; // This is to prevent 404 errors on page 2,3,4 if posts for that page dont exist.
include( TEMPLATEPATH . \'/author.php\' );
exit;
}
}
它用于搜索帖子的“writer”元值并显示作者。php模板。
url看起来像mysite。com/writer/WRITERNAME/pagination也适用于mysite。com/writer/WRITERNAME/page/2/可以正常工作。