我有一个很长时间的问题,期待你的帮助。我有一个自定义的“城市”分类法(它不像标记那样具有层次结构)。我只希望我的URL如下所示:
/%city%/%category%/%postname%/
这就是我想要完成的。
http://example.com/city/moscow
- 显示分类城市莫斯科的所有帖子http://example.com/category/shopping
- 显示此类别中的所有帖子和所有城市http://example.com/category/shopping/supermarket
- 显示此类别中的所有帖子和所有城市http://example.com/moscow/shopping/
-在莫斯科显示此类别的所有帖子http://example.com/moscow/shopping/supermarket/
- 在莫斯科显示此子类别中的所有帖子http://example.com/moscow/shopping/supermarket/postname
- 显示帖子
I’ve made the following:
首先,我创建了一个自定义分类法-城市我已经在函数中添加了代码。php
function add_custom_taxonomies_city() {
register_taxonomy(\'city\', \'post\', array(
\'hierarchical\' => false,
\'labels\' => array(
\'name\' => _x( \'City\', \'taxonomy general name\' ),
\'search_items\' => __( \'Search cities\' ),
\'all_items\' => __( \'All cities\' ),
\'parent_item\' => __( \'Parent Categories\' ),
\'parent_item_colon\' => __( \'Parent Categories:\' ),
\'edit_item\' => __( \'Edit city\' ),
\'update_item\' => __( \'Update city\' ),
\'add_new_item\' => __( \'Add city\' ),
\'new_item_name\' => __( \'New city\' ),
\'menu_name\' => __( \'cities\' ),
);
),
\'rewrite\' => array(
\'slug\' => \'city\',
\'with_front\' => false,
\'hierarchical\' => false
),
));
}
add_action( \'init\', \'add_custom_taxonomies_city\', 0 );
add_filter(\'post_link\', \'town_permalink\', 10, 3);
add_filter(\'post_type_link\', \'town_permalink\', 10, 3);
function town_permalink($permalink, $post_id, $leavename) {
if (strpos($permalink, \'%city%\') === FALSE) return $permalink;
// Get post
$post = get_post($post_id);
if (!$post) return $permalink;
// Get taxonomy terms
$terms = wp_get_object_terms($post->ID, \'city\');
if (!is_wp_error($terms) && !empty($terms) && is_object($terms[0])) $taxonomy_slug = $terms[0]->slug;
else $taxonomy_slug = \'not-city\';
return str_replace(\'%city%\', $taxonomy_slug, $permalink);
}
The result:
function addRoutes() {
add_rewrite_tag(\'%city%\', \'(.+?)\');
add_rewrite_rule(\'(.+?)/(.+?)/([^/]+)(/[0-9]+)?/?$\', \'index.php?city=$matches[1]&category_name=$matches[2]&name=$matches[3]&page=$matches[4]\', \'top\');
add_rewrite_rule(\'(.?.+?)(/[0-9]+)?/?$\', \'index.php?pagename=$matches[1]&page=$matches[2]\', \'top\');
flush_rewrite_rules();
}
add_action(\'init\', \'addRoutes\');
As a result http://example.com/city/moscow
- 找不到页面404错误。http://example.com/category/shopping
- 找不到页面404错误。。http://example.com/category/shopping/supermarket/
- 找不到页面404错误。。http://example.com/moscow/shopping/supermarket/postname
–400错误
系统似乎不明白%city%怎么了?期待你的帮助有人能帮我吗?非常感谢!
我决定将URL的结构更改为:
example.com/city/moscow/category/shopping/
-在莫斯科显示此类别的所有帖子example.com/city/moscow/category/shopping/supermarket/
- 在莫斯科显示此子类别中的所有帖子example.com/city/moscow/category/shopping/supermarket/postname
- 显示帖子
代码:
//The rewrite rules
function eg_add_query_vars( $query_vars ) {
$new_vars = array( \'town\' );
return array_merge( $new_vars, $query_vars );
}
add_filter( \'query_vars\', \'eg_add_query_vars\' );
function eg_add_rewrite_rules() {
global $wp_rewrite;
$new_rules = array(
\'city/(.+?)/category/(.+?)/?$\' => \'index.php?city=\' . $wp_rewrite->preg_index(1) . \'&category_name=\' . $wp_rewrite->preg_index(2),
\'city/(.+?)/category/(.+?)/(.+?)/?$\' => \'index.php?city=\' . $wp_rewrite->preg_index(1) . \'&category_name=\' . $wp_rewrite->preg_index(2) . \'&category_name=\'. $wp_rewrite->preg_index(3),
//\'city/(.+?)/category/(.+?)/(.+?)/(.+?)/?$\' => \'index.php?city=\' . $wp_rewrite->preg_index(1) . \'&category_name=\' . $wp_rewrite->preg_index(2) . \'&category_name=\'. $wp_rewrite->preg_index(3). \'&page=\'. $wp_rewrite->preg_index(4),// This is me effort to write the rule for posts (doesn\'t work)
);
$wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
}
add_action( \'generate_rewrite_rules\', \'eg_add_rewrite_rules\' );
//creating the permalink:
function city_permalink($permalink, $post_id, $leavename) {
if (strpos($permalink, \'%city%\') === FALSE) return $permalink;
// Get post
$post = get_post($post_id);
if (!$post) return $permalink;
// Get taxonomy terms
$terms = wp_get_object_terms($post->ID, \'town\');
if (!is_wp_error($terms) && !empty($terms) && is_object($terms[0])) $taxonomy_slug = $terms[0]->slug;
else $taxonomy_slug = \'not-city\';
return str_replace(\'%town%\', $taxonomy_slug, $permalink);}
function wp_town_query( $query ) {
if( isset( $query->query_vars[\'city\'] ) ):
if( $city = get_term_by( \'id\', $query->query_vars[\'city\'], \'city\' ) )
$query->query_vars[\'town\'] = $city->slug;
endif;
}
add_action( \'parse_query\', \'wp_city_query\' );
Permalink管理页面中的我的设置:城市/%city%/类别/%category%/%postname%/
因此,我有:-example.com/city/moscow/category/shopping/
-工程-example.com/city/moscow/category/shopping/supermarket/
- 工作,但我仍然不明白如何为帖子编写规则:-example.com/city/moscow/category/shopping/supermarket/postname
- 不起作用。分页也不起作用。
我很乐意得到任何建议!
这是工作:
\'city/(.+?)/category/(.+?)/(.+?)/([^/]+)/?$\' => \'index.php?city=\' . $wp_rewrite->preg_index(1) . \'&category_name=\' . $wp_rewrite->preg_index(2). \'&category_name=\' . $wp_rewrite->preg_index(3). \'&name=\' . $wp_rewrite->preg_index(4),
\'city/(.+?)/category/(.+?)/page/?([0-9]{1,})/?$\' => \'index.php?city=\' . $wp_rewrite->preg_index(1) . \'&category_name=\' . $wp_rewrite->preg_index(2). \'&paged=\' . $wp_rewrite->preg_index(3),
\'city/(.+?)/category/(.+?)/?$\' => \'index.php?city=\' . $wp_rewrite->preg_index(1) . \'&category_name=\' . $wp_rewrite->preg_index(2),