我创建了1个带有2个分类法的自定义帖子类型(项目)(clients / fields) 过滤结果(如类别)。
最后,应该是这样的,其中project\\u name是在单个项目中显示的同一个自定义帖子。php:
我的网站/项目/我的网站/项目/我的项目/我的项目/我的网站/我的项目/我的领域/我的网站/我的项目/我的领域/我的网站/我的项目/我的项目/我的客户/我的网站/我的项目/我的客户/我的项目,我将能够轻松地显示当前选择的术语,并在这个特定术语内导航每篇文章(在single projects.php中)。
我正在使用存档显示所有自定义帖子:archive-projects.php.
自定义贴子永久链接如下所示:my-site/projects/project_name
我有一个分类模板,可以显示与术语相关的自定义帖子,它运行良好:taxonomy.php
到目前为止还不错。
但permalinks仍然是这样的:my-site/projects/project_name.
我想根据当前的分类法为他们提供一个不同的permalink,并获得上面所写的不同URL。
我已经找到了this 它在标准情况下工作得很好(例如,一个没有几个类别的博客),但当我尝试适应分类法时,我陷入了困境。我可以创建新的url,但无法将其提供给permalink帖子。
有什么帮助或建议吗?谢谢
[UPDATE]以下是我迄今为止所做的工作:
function multiple_taxonomy_post_link($CPT_url = ){
// check permalink structure for the required construct; /%category%/%postname%/
if (strrpos(get_option(\'permalink_structure\'), \'%category%/%postname%\') !== false){
// get the current post
global $post, $wp_query;
// prepare variables for use below
$post_id = $term_id = 0;
$new_CPT_url = \'\';
// for taxonomies
if (is_tax())
{
// remember current category and post
$term_obj = get_term_by( \'slug\', get_query_var( \'term\' ), get_query_var( \'taxonomy\' ) );
$term_id = $term_obj->term_id;
$post_id = $post->ID;
// add the post slug to the current url
$new_CPT_url = $_SERVER[\'REQUEST_URI\'] . $post->post_name;
}
else if (is_singular(\'projects\'))
{
$term_object = get_term_by( \'slug\', get_query_var( \'term\' ), get_query_var( \'taxonomy\' ) );
// remember current category and post
$post_id = $wp_query->post->ID;
if ($term_object) $term_id = $term_object->term_id;
// replace the slug of the post being viewed by the slug of $post
$new_CPT_url = $_SERVER[\'REQUEST_URI\'].$post->post_name;
}
if ($post_id > 0 && $term_id > 0 && !empty($new_CPT_url))
{
$current_tax = get_query_var( \'taxonomy\' );
$terms = get_the_terms( $post->ID, $current_tax );
// make sure categories match!
foreach($terms as $term)
{
if ($term->term_id == $term_id)
{
$CPT_url = $new_CPT_url;
break;
}
}
}
}
// always return an url!
return $CPT_url;
}
add_filter(\'post_type_link\', \'multiple_taxonomy_post_link\');
它很好地显示了分类法模板上的新永久链接,重定向指向了所需的帖子,但WP一直将单个帖子url写为
my-site/projects/project_name. 是因为目标帖子是自定义帖子类型吗?
SO网友:vmassuchetto
我即将发布一个插件来实现这一点(以及其他一些事情)。你必须修改你的重写规则。此代码适用于类别帖子。也许,如果您对它稍加研究,它将使您获得与post类型和分类法相同的行为。
首先,生成所需的规则:
function get_category_rules() {
$rules = array();
/*
* Will generate patterns for:
* (category)/
* category/(subcategory)
* category/subcategory/(post)
* feed/(category)
* feed/category/(subcategory)
*/
$feed_sufix = \'/feed/?(rss|rss2|atom)?/?$\';
$page_sufix = \'/page/?([0-9]{1,})?/?$\';
/* Walk within the categories by level */
$categories = get_categories(array(
\'orderby\' => \'slug\',
\'hide_empty\' => false,
\'exclude\' => implode(\',\', $excludes)
));
foreach($categories as $category) {
$level = 0;
$path = \'(\' . $category->slug . \')\';
while (($category = get_category($category->parent)) && isset($category->term_id)) {
$level++;
$path = $category->slug . \'/\' . $path;
}
// Feeds
$rules[$level][$path . $feed_sufix] = \'index.php?category_name=$matches[1]&feed=atom\';
// Category archives
$rules[$level][$path . \'/?$\'] = \'index.php?category_name=$matches[1]\';
// Category archive pages
$rules[$level][$path . $page_sufix] = \'index.php?category_name=$matches[1]&paged=$matches[2]\';
// Posts
$rules[$level][$path . \'/([^/]+)/?$\'] = \'index.php?name=$matches[2]\';
}
$retval = array();
for ($i = count($rules) - 1; $i >= 0; $i--) {
$retval = array_merge($retval, $rules[$i]);
}
return $retval;
}
然后,将这些规则与现有规则合并:
add_action(\'rewrite_rules_array\', \'rewrite_rules\');
function rewrite_rules($rules) {
$category_rules = get_category_rules();
$rules = array_merge($category_rules, $rules);
return $rules;
}
和make
*_permalink
函数与之配合使用。
add_filter(\'post_link\', \'custom_post_permalink\');
function custom_post_permalink ($post_link) {
global $post;
$cats = array_reverse(get_the_category($post->ID));
if (!isset($cats[0]))
return $post_link;
$category = $cats[0];
$path = $category->slug;
while (($category = get_category($category->parent)) && isset($category->term_id)) {
$path = $category->slug . \'/\' . $path;
}
return str_replace(home_url(), home_url() . \'/\' . $path, $post_link);
}