我有一个名为目的地的自定义帖子类型和一个名为区域的自定义分类(包含亚洲,欧洲,非洲等字词。 )我创建了一个目的地(威尼斯),并将其与一个地区(欧洲)相关联。现在我已经花了好几个小时让CPT的永久链接像/%region%/%destination%
一样工作(例如http://example.com/europe/venice),但它一直给我 404 Not found 。这是相关的代码:
add_action("init", "register_region_taxonomy");
add_action("init", "create_destinations_post_type");
add_filter("post_type_link", "region_permalink", 10, 3);
这就是我创建自定义分类法的方法:
function register_region_taxonomy() {
$labels = array(
"name" => _x("Destination Region", "taxonomy general name"),
"singular_name" => _x("Region", "taxonomy singular name"),
"search_items" => __("Search Regions"),
"popular_items" => __("Popular Regions"),
"all_items" => __("All Regions"),
"parent_item" => null,
"parent_item_colon" => null,
"edit_item" => __("Edit Region"),
"update_item" => __("Update Region"),
"add_new_item" => __("Add New Region"),
"new_item_name" => __("New Region"),
"separate_items_with_commas" => __("Separate regions with commas"),
"add_or_remove_items" => __("Add or remove regions"),
"choose_from_most_used" => __("Choose from the most used regions"),
"menu_name" => __("Regions"),
);
register_taxonomy("regions", "destination", array(
"hierarchical" => false,
"labels" => $labels,
"show_ui" => true,
"show_admin_column" => true,
"update_count_callback" => "_update_post_term_count",
"query_var" => "region",
"rewrite" => array("slug" => "region"),
));
}
这是我的自定义帖子类型:
function create_destinations_post_type() {
register_post_type("destination",
// CPT Options
array(
"labels" => array(
"name" => __("destinations"),
"singular_name" => __("destination")
),
"public" => true,
"publicly_queryable" => true,
"has_archive" => true,
"show_ui" => false,
"not_found" => "No destinations Found",
"rewrite" => array("slug" => "%region%", "with_front" => false)
)
);
}
这就是我希望它改变目的地的固定链接的方式:
function region_permalink($permalink, $post_id, $leavename) {
if (strpos($permalink, "%region%") === FALSE)
return $permalink;
// Get post
$post = get_post($post_id);
if (!$post)
return $permalink;
// Get taxonomy terms
$terms = wp_get_object_terms($post->ID, "regions");
if (!is_wp_error($terms) && !empty($terms) && is_object($terms[0]))
$taxonomy_slug = $terms[0]->slug;
else
$taxonomy_slug = "no-region";
return str_replace("%region%", $taxonomy_slug, $permalink);
}
我一步一步跟着this article,但无济于事。我尝试转到固定链接设置页面并再次保存 - 如上所述[{3}} - 但无效(我的永久链接设置设置为帖子名称)。我在创建CPT后尝试调用flush_rewrite_rules()
,但是不行,它只是不起作用。我不确定我是否能够正确使用here,因为我改变了这样的解决方案:
add_filter("rewrite_rules_array", "add_destination_rewrite_rules");
function add_destination_rewrite_rules($rules) {
$new = array();
$new['([^/]+)/(.+)/?$'] = 'index.php?destination=$matches[1]';
$new['(.+)/?$'] = 'index.php?region=$matches[0]';
return array_merge($new, $rules); // Ensure our rules come first
}
还没有成功,所以我现在已经恢复了这一改变。
如果我尝试this SO post,它确实会进入永久链接钩子,我的浏览器上的网址会更改为http://example.com/venice,但最终会向我显示那个邪恶的404页面。我在这里做错了什么线索?我的.htaccess看起来像这样:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /wpsite/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /wpsite/index.php [L]
</IfModule>
# END WordPress