如何创建自定义分类和自定义帖子类型(如base-name/parent-tax/child-tax/custom-post-type-name)的固定链接

时间:2013-05-27 作者:Noman

我发现this 链接太有用了。这是我迄今为止编写的代码,它适用于除两个场景之外的所有场景。

register_post_type( \'article\',
    array(
        \'labels\' => array(
            \'name\' => \'Article\',
            \'singular_name\' => \'Article\',
            \'add_new\' => \'Add Article\',
            \'add_new_item\' => \'Add New Article\',
            \'edit\' => \'Edit\',
            \'edit_item\' => \'Edit Article\',
            \'new_item\' => \'New Article\',
            \'view\' => \'View\',
            \'view_item\' => \'View Article\',
            \'search_items\' => \'Search Article\',
            \'not_found\' => \'No Article\',
            \'not_found_in_trash\' => \'No Post found in Article\',
            \'parent\' => \'Parent Article\'
        ),

        \'public\' => true,
        \'hierarchical\' => true,
        \'menu_position\' => 15,
        \'supports\' => array( \'title\', \'editor\', \'comments\', \'thumbnail\' ),
        \'taxonomies\' => array( \'\' ),
        \'menu_icon\' => get_template_directory_uri().\'/img/icon_article.png\',
        \'query_var\' => true,
        \'rewrite\' => array(
            \'slug\' => \'articles/%article_category%\',
            \'with_front\' => true
        ),
        \'has_archive\' => \'articles\',
        \'register_meta_box_cb\' => \'add_article_metaboxes\'
    )
);



register_taxonomy(\'article_category\',\'article\',array(
    \'hierarchical\' => true,
    \'labels\' => $labels,
    \'query_var\' => true,
    \'rewrite\' => array(\'slug\' => \'articles\', \'hierarchical\' => true),
  ));

add_filter(\'rewrite_rules_array\', \'mmp_rewrite_rules\');
function mmp_rewrite_rules($rules) {
    $newRules  = array();
    $newRules[\'articles/(.+)/(.+)/(.+)/?$\'] = \'index.php?article=$matches[3]\'; 
    $newRules[\'articles/(.+)/(.+)/?$\'] = \'index.php?article=$matches[2]\';
    $newRules[\'articles/(.+)/?$\'] = \'index.php?article_category=$matches[1]\'; 

    return array_merge($newRules, $rules);
}

function filter_post_type_link($link, $post)
{
    if ($post->post_type != \'article\')
        return $link;

    if ($cats = get_the_terms($post->ID, \'article_category\'))
    {
        $link = str_replace(\'%article_category%\', get_taxonomy_parents(array_pop($cats)->term_id, \'article_category\', false, \'/\', true), $link); // see custom function defined below
    }
    return $link;
}
add_filter(\'post_type_link\', \'filter_post_type_link\', 10, 2);

// my own function to do what get_category_parents does for other taxonomies
function get_taxonomy_parents($id, $taxonomy, $link = false, $separator = \'/\', $nicename = false, $visited = array()) {    
    $chain = \'\';   
    $parent = &get_term($id, $taxonomy);

    if (is_wp_error($parent)) {
        return $parent;
    }

    if ($nicename)    
        $name = $parent -> slug;        
    else    
        $name = $parent -> name;

    if ($parent -> parent && ($parent -> parent != $parent -> term_id) && !in_array($parent -> parent, $visited)) {    
        $visited[] = $parent -> parent;    
        $chain .= get_taxonomy_parents($parent -> parent, $taxonomy, $link, $separator, $nicename, $visited);

    }

    if ($link) {
        // nothing, can\'t get this working :(
    } else {   
        $chain .= $name . $separator;    
    }
    return $chain;    
}
但这种解决方案在两种情况下都不起作用。

第一种情况:在子分类法上直接链接,例如:sitename。com/articles/parenttax/childtax/

第二种情况:对自定义贴子页面进行分页,例如:sitename。com/articles/parenttax/childtax/postname/page/2

我是否必须为它们创建单独的函数,还是只需要为其添加新规则?

1 个回复
SO网友:Matthew Boynes

创建永久链接结构时,如/article/%category%/%postname%/, 当%category% 是分层的。WordPress遇到的问题是知道在第二个斜杠之后,URL的下一部分是类别还是帖子。例如,在url中/article/parent/child/, “child”是“parent”的子类别,还是一个名为“child”的帖子?在你的情况下,情况更糟,因为你的文章也是分层的。

最好是在两者之间添加一个可预测的或静态的分隔符。以下是一些示例:

/%article_category%/articles/%article%/
/articles/%article_category%/article/%article%/
/articles/%article_category%/%year%/%article%/
如果您坚持维护您提议的结构,这不是不可能的,但它需要一些远远超出Q&;范围的自定义编程;像这样的网站。

结束

相关推荐

Taxonomy based permalinks

在我的网站上,我使用自己的分类法。我希望分类学与permalink相结合。因此,如果一个新闻页面有分类法,permalink就会改变。像这样:mysite。com/%custom\\u tax%/blog/%postname%,如果是一个页面,请点击:mysite。com/%custom\\u tax%/%postname%但如果没有我想要的分类法:mysite。com/blog/%postname%,如果它是一个页面:mysite。com/%postname%我怎样才能轻松地做到这一点?我已经设置了%c