将分类添加到自定义帖子类型归档

时间:2013-09-21 作者:Chris Janzen

这是我的第一个问题,请耐心听我说。我正在尝试设置一个自定义的帖子类型,它有一些与之相关的分类法。每个分类法都是分层的。我想要实现的是一个比现在更好的url结构。我的永久链接设置为默认的“帖子名称”设置。我想要实现的是这样一个目标:领域。com/自定义帖子类型/分类法/类型

最终我得到的是:域名。com/分类法/类型

这本质上是一个职业委员会,我有不同的分类法代表不同的类别。例如,不同类型的工作都有“就业部门”。是否应将这些设置为层次结构?

当我使用一个名为“Custom Post Type Permalinks”的插件时,我可以让它以我想要的方式工作,但我想知道如果没有插件,它是否可能。

以下是我创建的自定义帖子类型插件的代码:

<?php
add_action( \'init\', \'career_forum\' );
// let\'s create the function for the custom type
function career_forum() { 
    $labels = array(
        \'name\' => _x( \'Careers\', \'post type general name\' ),
        \'singular_name\' => _x( \'Career Listing\', \'post type singular name\' ),
        \'add_new\' => __( \'Add New\', \'career\' ),
        \'add_new_item\' => __( \'Add New Career Listing\' ),
        \'edit_item\' => __( \'Edit Career Listing\' ),
        \'new_item\' => __( \'New Career Listing\' ),
        \'view_item\' => __( \'View Career Listing\' ),
        \'search_items\' => __( \'Search Careers\' ),
        \'not_found\' => __( \'No careers found\' ),
        \'not_found_in_trash\' => __( \'No careers found in trash\' ),
        \'parent_item_colon\' => \'\'
    );
    $args = array( 
        \'labels\' => $labels,
        \'public\' => true,
        \'publicly_queryable\' => true,
        \'show_ui\' => true,
        \'query_var\' => true,
        \'rewrite\' => true,
        \'capability_type\' => \'post\',
        \'hierarchical\' => true,
        \'has_archive\' => true,
        \'menu_position\' => 5,
        \'rewrite\' => array( \'slug\' => \'careers\', \'with_front\' => false ),
        \'supports\' => array( \'title\', \'editor\', \'thumbnail\')
);

register_post_type( \'career_listings\', $args );
}
add_action( \'init\', \'create_career_forum_taxonomies\', 0 );
// function for adding taxonomies for Career Forum
function create_career_forum_taxonomies() {
    // Job Sectors
    $labels = array(
        \'name\' => _x( \'Job Sectors\', \'taxonomy general name\' ),
        \'singular_name\' => _x( \'Job Sector\', \'taxonomy singular name\' ),
        \'search_items\' => __( \'Search Job Sectors\' ),
        \'all_items\' => __( \'All Job Sectors\' ),
        \'parent_item\' => __( \'parent Job Sector\' ),
        \'parent_item_colon\' => __( \'Parent Job Sector:\' ),
        \'edit_item\' => __( \'Edit Job Sector\' ),
        \'update_item\' => __( \'Update Job Sector\' ),
        \'add_new_item\' => __( \'Add New Job Sector\' ),
        \'new_item_name\' => __( \'New Job Sector Name\' ),
    );

register_taxonomy( \'job_sector\', 
    array( \'career_listings\' ), 
    array( 
    \'hierarchical\' => true,
    \'labels\' => $labels,
    \'show_ui\' => true,
    \'query_var\' => true,
    //\'rewrite\' => array( \'slug\' => \'job-sector\', \'hierarchical\' => true, \'with_front\' => true ),
    )
);

// Regions
$labels = array(
    \'name\' => _x( \'Regions\', \'taxonomy general name\' ),
    \'singular_name\' => _x( \'Region\', \'taxonomy singular name\' ),
    \'search_items\' => __( \'Search Regions\' ),
    \'all_items\' => __( \'All Regions\' ),
    \'parent_item\' => __( \'parent Region\' ),
    \'parent_item_colon\' => __( \'Parent Region:\' ),
    \'edit_item\' => __( \'Edit Region\' ),
    \'update_item\' => __( \'Update Region\' ),
    \'add_new_item\' => __( \'Add New Region\' ),
    \'new_item_name\' => __( \'New Region Name\' ),
);

register_taxonomy( \'region\', 
    array( \'career_listings\' ), 
    array( 
    \'hierarchical\' => true,
    \'labels\' => $labels,
    \'show_ui\' => true,
    \'query_var\' => true,
    \'rewrite\' => array( \'slug\' => \'region\', \'hierarchical\' => true ),
    )
);

// Position Type
$labels = array(
    \'name\' => _x( \'Position Types\', \'taxonomy general name\' ),
    \'singular_name\' => _x( \'Position Type\', \'taxonomy singular name\' ),
    \'search_items\' => __( \'Search Position Types\' ),
    \'all_items\' => __( \'All Position Types\' ),
    \'parent_item\' => __( \'parent Position Type\' ),
    \'parent_item_colon\' => __( \'Parent Position Type:\' ),
    \'edit_item\' => __( \'Edit Position Type\' ),
    \'update_item\' => __( \'Update Position Type\' ),
    \'add_new_item\' => __( \'Add New Position Type\' ),
    \'new_item_name\' => __( \'New Position Type Name\' ),
);

register_taxonomy( \'position_type\', 
    array( \'career_listings\' ), 
    array( 
    \'hierarchical\' => true,
    \'labels\' => $labels,
    \'show_ui\' => true,
    \'query_var\' => true,
    \'rewrite\' => array( \'slug\' => \'position-type\', \'hierarchical\' => true ),
    )
);
}

1 个回复
最合适的回答,由SO网友:Eugenious 整理而成

嗯,你就不能随便摆弄一下重写段吗?

因此,您可以使用其他url来代替“slug”=>“position type”,即“slug”=>“/职业/职位类型”。

在执行此操作时,可能需要禁用自定义帖子类型的重写。

另外,别忘了刷新重写规则,以使更改生效,我花了一段时间才弄清楚。基本上可以通过查看管理站点中的permalinks页面来完成。

结束

相关推荐

wordpress permalinks tweeks

在创建任何帖子后,我都会遵循permalink结构。。。http://domain.com/wp/postname根据Permalink设置。。。。职位名称:http://domain.com/wp/sample-post/这个永久链接对于页面来说很好,但我如何才能像下面这样添加blog作为前缀。。。。http://domain.com/wp/blog/文章标题http://domain.com/wp/blog/类别/职位名称如果你需要更多信息,请告诉我,谢谢。