我正在尝试修改 WordPress(版本 3.3)以列出具有一种类别和一种分类法的帖子。
我有一个称为“位置”的分类法。如果我执行 example.org/location/canada,它就会起作用。现在我想要重写 example.org/location/canada/category/dogs 的网址,但我无法实现。
我在functions.php中添加了这段代码:
函数eg_add_rewrite_rules() { 全局 $wp_rewrite;
$new_rules = array(
'location/(.+)/category/(.+)/?$' => 'index.php?location=' . $wp_rewrite->preg_index(1) . '&category_name=' . $wp_rewrite->preg_index(2)
);
$wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
} add_action( 'generate_rewrite_rules', 'eg_add_rewrite_rules' );
我还添加了这个:
函数eg_add_query_vars( $query_vars ) { $new_vars = array( '位置' );
return array_merge( $new_vars, $query_vars );
} add_filter( 'query_vars', 'eg_add_query_vars' );
类别是分类法。所以这取决于您想在那里使用什么分类法。标签、类别、自定义分类法。
如果您想创建自定义分类法,您可以。查看 http://codex.wordpress.org/Taxonomies
这是注册自定义分类法的示例。
function people_init() {
// create a new taxonomy
register_taxonomy(
'people',
'post',
array(
'label' => __( 'People' ),
'rewrite' => array( 'slug' => 'person' ),
'capabilities' => array('assign_terms'=>'edit_guides', 'edit_terms'=>'publish_guides')
)
);
} add_action( 'init', 'people_init' );
然后您需要编辑您的永久链接。转到设置> 固定链接。使用自定义更改 URL 以反映您希望路径的显示方式。
如果您愿意,您可以在此处输入类别和标签 URL 的自定义结构。例如,使用主题作为类别基础将使您的类别链接类似于 http://example.org/topics/uncategorized/。如果将这些留空,将使用默认值。
问题已解决:
在functions.php中(或者如果你想要的话在插件中)。
您必须输入此代码来重写此网址 www.example.org/en/[mylocation]/de/[mycategory]
add_action('init', 'flush_rewrite_rules');
add_filter('category_rewrite_rules' , 'add_rules' ) ;
function flush_rules() {
global $wp_rewrite;
$wp_rewrite->flush_rules();
}
function add_rules($rules)
{
/**
* Loop em.
* -------------------------------------------- */
$feed_rule = 'index.php?location=$matches[1]&category_name=$matches[2]&feed=$matches[3]';
$paged_rule = 'index.php?location=$matches[1]&category_name=$matches[2]&paged=$matches[3]';
$base_rule = 'index.php?location=$matches[1]&category_name=$matches[2]';
$rules['en/([^/]+)/de/([^/]+)/(?:feed/)?(feed|rdf|rss|rss2|atom)/?$'] = $feed_rule;
$rules['en/([^/]+)/de/([^/]+)/page/?([0-9]{1,})/?$'] = $paged_rule;
$rules['en/([^/]+)/de/([^/]+)/?$'] = $base_rule;
$feed_rule2 = 'index.php?location=$matches[1]&feed=$matches[2]';
$paged_rule2 = 'index.php?location=$matches[1]&paged=$matches[2]';
$base_rule2 = 'index.php?location=$matches[1]';
$rules['en/([^/]+)/(?:feed/)?(feed|rdf|rss|rss2|atom)/?$'] = $feed_rule2;
$rules['en/([^/]+)/page/?([0-9]{1,})/?$'] = $paged_rule2;
$rules['en/([^/]+)/?$'] = $base_rule2;
return $rules;
}