在两个自定义分类法"city"
下创建了一个名为"continent" & "country"
的自定义帖子类型
“欧洲”是"continent"
“西班牙”是"country"
我更改了永久链接结构。
自定义重写代码:/%continent%/%country%
所以我的永久链接看起来像这样mysite.com/europe/spain/madrid/
add_filter('post_link', 'continent_permalink', 10, 3);
add_filter('post_type_link', 'continent_permalink', 10, 3);
function continent_permalink($permalink, $post_id, $leavename) {
if (strpos($permalink, '%continent%') === FALSE) return $permalink;
$post = get_post($post_id);
if (!$post) return $permalink;
$terms = wp_get_object_terms($post->ID, 'continent');
if ( !is_wp_error($terms) && !empty($terms) && is_object($terms[0]) )
$taxonomy_slug = $terms[0]->slug;
else
$taxonomy_slug = 'continent';
return str_replace('%continent%', $taxonomy_slug, $permalink);
}
add_filter('post_link', 'country_permalink', 10, 3);
add_filter('post_type_link', 'country_permalink', 10, 3);
function country_permalink($permalink, $post_id, $leavename) {
if (strpos($permalink, '%country%') === FALSE) return $permalink;
$post = get_post($post_id);
if (!$post) return $permalink;
$terms = wp_get_object_terms($post->ID, 'country');
if ( !is_wp_error($terms) && !empty($terms) && is_object($terms[0]) )
$taxonomy_slug = $terms[0]->slug;
else
$taxonomy_slug = 'country';
return str_replace('%country%', $taxonomy_slug, $permalink);
}
现在我想在城市发布后写上virtual pages
,它是“马德里”的,看起来像这样
mysite.com/europe/spain/madrid/archives
mysite.com/europe/spain/madrid/features
mysite.com/europe/spain/madrid/details
但是似乎无法使它工作
当我这样操作时就可以使用
mysite.com/europe/spain/madrid/?details=archives
但是如果我像这样fdo它就行不通了
mysite.com/europe/spain/madrid/archives (going to 404)
这是我用来添加虚拟页面的代码
function example_details_query_vars($vars) {
$vars[] = 'details';
return $vars;
}
add_filter('query_vars', 'example_details_query_vars');
function example_details_add_rewrite_rules() {
add_rewrite_tag('%details%', '([^&]+)');
add_rewrite_rule(
'archives/?$',
'index.php?details=archives',
'bottom'
);
}
add_action('init', 'example_details_add_rewrite_rules');
/**
* Assign templates to the virtual pages.
*/
function example_details_template_include($template) {
global $wp_query;
$new_template = '';
if (array_key_exists('details', $wp_query->query_vars)) {
switch ($wp_query->query_vars['details']) {
case 'archives':
$new_template = locate_template(array(
'custom.php'
));
break;
}
if ($new_template != '') {
return $new_template;
} else {
$wp_query->set_404();
status_header(404);
return get_404_template();
}
}
return $template;
}
add_filter('template_include', 'example_details_template_include');