不同的存档和单个固定链接URL

时间:2015-11-17 作者:Callum Bonnyman

我有一个自定义的帖子类型来列出国家/地区,但我不确定如何使用这种永久链接结构创建它们:

国家档案馆:/map/map/country/england

function countries() {

    $labels = array(
        \'name\'                  => _x( \'countries\', \'Post Type General Name\', \'countries\' ),
        \'singular_name\'         => _x( \'country\', \'Post Type Singular Name\', \'countries\' ),
        \'menu_name\'             => __( \'Countries\', \'countries\' ),
        \'name_admin_bar\'        => __( \'Country\', \'countries\' ),
        \'parent_item_colon\'     => __( \'Parent Country\', \'countries\' ),
        \'all_items\'             => __( \'All Countries\', \'countries\' ),
        \'add_new_item\'          => __( \'Add New Country\', \'countries\' ),
        \'add_new\'               => __( \'Add New Country\', \'countries\' ),
        \'new_item\'              => __( \'New Country\', \'countries\' ),
        \'edit_item\'             => __( \'Edit Country\', \'countries\' ),
        \'update_item\'           => __( \'Update Country\', \'countries\' ),
        \'view_item\'             => __( \'View Country\', \'countries\' ),
        \'search_items\'          => __( \'Search Countries\', \'countries\' ),
        \'not_found\'             => __( \'Country Not found\', \'countries\' ),
        \'not_found_in_trash\'    => __( \'Not found in Trash\', \'countries\' ),
        \'items_list\'            => __( \'Country list\', \'countries\' ),
        \'items_list_navigation\' => __( \'Countries List Navigation\', \'countries\' ),
        \'filter_items_list\'     => __( \'Filter Country List\', \'countries\' ),
    );
    $rewrite = array(
        \'slug\'                  => \'country\',
        \'with_front\'            => true,
        \'pages\'                 => true,
        \'feeds\'                 => true,
    );
    $args = array(
        \'label\'                 => __( \'country\', \'countries\' ),
        \'description\'           => __( \'Country Information\', \'countries\' ),
        \'labels\'                => $labels,
        \'supports\'              => array( \'title\', \'editor\', \'excerpt\', \'thumbnail\', \'trackbacks\', \'revisions\', \'custom-fields\', \'page-attributes\', ),
        \'taxonomies\'            => array( \'category\', \'post_tag\' ),
        \'hierarchical\'          => false,
        \'public\'                => true,
        \'show_ui\'               => true,
        \'show_in_menu\'          => true,
        \'menu_position\'         => 5,
        \'show_in_admin_bar\'     => true,
        \'show_in_nav_menus\'     => true,
        \'can_export\'            => true,
        \'has_archive\'           => true,        
        \'exclude_from_search\'   => false,
        \'publicly_queryable\'    => true,
        \'rewrite\'               => $rewrite,
        \'capability_type\'       => \'page\',
    );
    register_post_type( \'countries\', $args );

}
add_action( \'init\', \'countries\', 0 );
我也在尝试以下分类法,但即使在我更新permalink设置页面时,它也是404:

register_taxonomy(
    \'region\',
    \'country\',
    [
        \'label\' => __( \'Region\' ),
        \'rewrite\' => array( \'slug\' => \'map/region\' ),
        \'hierarchical\' => true,
    ]
);

2 个回复
SO网友:Milo

要获取所需的存档URL,请设置has_archive\'map\' 而不是true.

要获取所需的单个帖子URL,请设置slug 您的$rewrite 阵列到\'map/country\'

SO网友:s_ha_dum

您只需要一个层次结构的帖子类型。改变\'hierarchical\' => false,\'hierarchical\' => true, 并适当地嵌套您的帖子

相关推荐