如何在层次分类URL中包含父术语?

时间:2014-07-21 作者:Traveler

对于所有自定义分类法,URL如下所示:/base/slug/.

我需要一个解决方案,以使我的层次分类法具有:/base/parentslug/childslug/.

有人知道如何重定向和/或拥有与类别相同的功能吗?

3 个回复
SO网友:Alexander Dolgopolskiy

只是澄清一下,Parst通过一个自定义分类注册码的工作示例指出了什么:

$labels = array(
        \'name\'              => _x( \'Issue numbers\', \'taxonomy general name\', \'sascha_setup_post_type\' ),
        \'singular_name\'     => _x( \'Issue number\', \'taxonomy singular name\', \'sascha_setup_post_type\' ),
        \'search_items\'      => __( \'Search issues\', \'sascha_setup_post_type\' ),
        \'all_items\'         => __( \'All issue numbers\', \'sascha_setup_post_type\' ),
        \'parent_item\'       => __( \'Year of publication\', \'sascha_setup_post_type\' ),
        \'parent_item_colon\' => __( \'Year of publication:\', \'sascha_setup_post_type\' ),
        \'edit_item\'         => __( \'Edit issue number\', \'sascha_setup_post_type\' ),
        \'update_item\'       => __( \'Update issue number\', \'sascha_setup_post_type\' ),
        \'add_new_item\'      => __( \'Add new issue number\', \'sascha_setup_post_type\' ),
        \'new_item_name\'     => __( \'New issue number\', \'sascha_setup_post_type\' ),
        \'menu_name\'         => __( \'Issue numbers\', \'sascha_setup_post_type\' ),
    );

    $args = array(
        \'hierarchical\'      => true,
        \'labels\'            => $labels,
        \'show_ui\'           => true,
        \'show_admin_column\' => true,
        \'query_var\'         => true,
        \'rewrite\'           => array( \'slug\' => \'issues\', \'hierarchical\' => true ),
    );

    register_taxonomy( \'issue_number\', array( \'post\', \'issue\' ), $args );
结果给出了一个类似www.yourdomain的url。com/issues/2016/04/-正如您所愿。和在实现了这一点之后,不要忘记刷新permalink规则,不要为自定义分类URL获取404。最简单的方法-转到Admin/Permalinks,只需点击“save”(保存)按钮,无需更改任何内容。

SO网友:Rarst

根据register_taxonomy() 有文档记录rewrite/hierarchical 控制该设置的设置。默认为false, 这可能就是为什么您看不到它与本地分类法的行为相同。

SO网友:Gendrith

为了更清楚地说明这一点,请像这样使用重写/分层"rewrite" => array("hierarchical" => true) 在$args上,数组将自动在url上添加taxomy的父段。

没有必要为每个术语添加规则。

function type() {
    $labels = array(
        "name" => "Type",
        "singular_name" => "Type",
        "menu_name" => "Type",
    );
    $args = array(
        "labels" => $labels,
        "hierarchical" => true,
        "public" => true,
        "show_ui" => true,
        "show_admin_column" => true,
        "show_in_nav_menus" => true,
        "show_tagcloud" => true,
        "show_in_rest" => true,
        "rewrite" => array("hierarchical" => true)
    );
    register_taxonomy("type", array("products"), $args);
}

结束

相关推荐

page is not redirecting

如果只能找到一篇帖子,我想重定向到我的主页。我尝试了下面的代码,但它不起作用,我也尝试了标题(“位置:url”),但它也不起作用。 <?php $args = array( \'post_type\' => \'post\', \'posts_per_page\'=> 10, \'post_status\' => \'publish\', ); $my_qu