是的,这是可能的。
首先,应该创建与WPML兼容的新rewrite\\u规则。此版本在URL中添加分类slug。(post\\u类型/分类法/术语)
/**
* Custom post type specific rewrite rules
* @return wp_rewrite Rewrite rules handled by Wordpress
*/
function cpt_rewrite_rules($wp_rewrite) {
$rules = cpt_generate_archives(\'your_post_type_name\', $wp_rewrite);
$wp_rewrite->rules = $rules + $wp_rewrite->rules;
return $wp_rewrite;
}
add_action(\'generate_rewrite_rules\', \'cpt_rewrite_rules\');
/**
* Generate archive rewrite rules for a given custom post type
* @param string $cpt slug of the custom post type
* @return rules returns a set of rewrite rules for Wordpress to handle
*/
function cpt_generate_archives($cpt, $wp_rewrite) {
$rules = array();
$post_type = get_post_type_object($cpt);
if ($post_type->has_archive === false)
return $rules;
$languages = icl_get_languages();
$taxonomies = get_taxonomies(array(\'_builtin\' => false), \'objects\');
$permalink_structs = array();
foreach ($taxonomies as $key => $tax) {
// get first associated taxonomy
if ($tax->object_type[0] == $cpt) {
$permalink_structs[] = array(
\'rule\' => "{$tax->rewrite[\'slug\']}/([^/]+)",
\'vars\' => array($tax->query_var)
);
break;
}
}
// generate for all languages
foreach ($languages as $lang) {
$slug_archive = WPML_Slug_Translation::get_translated_slug($post_type->rewrite[\'slug\'], $lang[\'language_code\']);
foreach ($permalink_structs as $data) {
$query = \'index.php?post_type=\'.$cpt;
$rule = $slug_archive.\'/\'.$data[\'rule\'];
$i = 1;
foreach ($data[\'vars\'] as $var) {
$query.= \'&\'.$var.\'=\'.$wp_rewrite->preg_index($i);
$i++;
}
$rules[$rule."/?$"] = $query;
if ($post_type->rewrite[\'feeds\']) {
$rules[$rule."/feed/(feed|rdf|rss|rss2|atom)/?$"] = $query."&feed=".$wp_rewrite->preg_index($i);
$rules[$rule."/(feed|rdf|rss|rss2|atom)/?$"] = $query."&feed=".$wp_rewrite->preg_index($i);
}
if ($post_type->rewrite[\'pages\']) {
$rules[$rule."/page/([0-9]{1,})/?$"] = $query."&paged=".$wp_rewrite->preg_index($i);
}
}
}
return $rules;
}
其次,您必须重写生成的term\\u链接:
/**
* Rewrite taxonomy term link to custom term link
* @param string $url The term URL
* @param string $term The term object
* @param string $taxonomy The taxonomy slug
* @return url returns term link url
*/
function rewrite_taxonomy_term_permalink($termlink, $term, $taxonomy) {
if (\'your_taxonomy_name\' == $taxonomy) {
$post_type = get_post_type_object(\'your_custom_post_type_name\');
$taxonomy_object = get_taxonomy($taxonomy);
$slug_archive = WPML_Slug_Translation::get_translated_slug($post_type->rewrite[\'slug\'], ICL_LANGUAGE_CODE);
return home_url() . $slug_archive . \'/\' . $taxonomy_object->rewrite[\'slug\'] . \'/\' . $term->slug . \'/\';
}
return $termlink;
}
add_filter( \'term_link\', \'rewrite_taxonomy_term_permalink\', 10, 3);
Vuala,它就像一个符咒:)
如果希望URL中没有分类名称。您必须绝对使用术语slug定义URL片段。否则,它将与single post type rewrite和get 404冲突(分类法的post\\u type/slug的定义晚于single的post\\u type/slug,因此具有更高的优先级并将搜索分类法术语)
有关实施的更多信息,请参见此处:http://someweblog.com/wordpress-custom-taxonomy-with-same-slug-as-custom-post-type/