在自定义分类URL中显示自定义投递类型

时间:2013-10-18 作者:YumYumPandaBurgers

我创建了一个带有两个自定义分类法的自定义帖子类型,最终将用于过滤自定义帖子类型中的结果。我遇到的问题是,我希望URL为:

网站url/自定义帖子类型/自定义分类法

我曾尝试在这里为有类似问题的人提供一些建议,但在这种情况下我无法使其工作。这是我目前拥有的代码,我已经将其剥离回去,因为尝试使其工作是徒劳的:

//Custom Post Types

add_action( \'init\', \'create_post_type\' );
function create_post_type() {
register_post_type( \'listing\',
    array(
        \'labels\' => array(
            \'name\' => __( \'Listings\' ),
            \'singular_name\' => __( \'Listing\' )
        ),
    \'public\' => true,
    \'has_archive\' => true,
    \'show_ui\' => true,
    \'capability_type\' => \'post\',
    \'hierarchical\' => false,
    \'rewrite\' => array( \'slug\' => \'directory\', \'with_front\' => false ),
    \'query_var\' => false,
    \'supports\' => array( \'title\', \'editor\' )
    )
);
}

//Region Taxonomies

function region_taxonomy() {  
register_taxonomy(  
\'region\',  
\'listing\',  
array(  
    \'hierarchical\' => true,  
    \'label\' => \'Regions\',  
    \'query_var\' => true,  
    \'rewrite\' => array(\'slug\' => \'region\', \'with_front\' => true )  
)  
);  
}  

add_action( \'init\', \'region_taxonomy\' );

//Qualifications Taxonomies
function qualification_taxonomy() {  
register_taxonomy(
\'qualification\',  
\'listing\',  
array(  
    \'hierarchical\' => true,  
    \'label\' => \'Qualifications\',  
    \'query_var\' => true,  
    \'rewrite\' => array(\'slug\' => \'qualification\' )  
)  
);  
}
目前,这将产生:

网站url/地区/或/资格/

如果有人能帮我解决这件事,那就太好了,浪费了很多时间到处转圈!

1 个回复
SO网友:neoian

您可以使用WordPress接受自定义重写规则的功能来完成此操作。您可以将以下内容添加到函数文件中,或创建插件(推荐)。

function url_rewrites_wpanswers() {
    add_rewrite_rule(\'/custom-post-type-slug/([^/]*)?$\',\'index.php?post_type=custom-post-type-name&custom-taxonomy-name=$matches[1]\', \'top\'); //lets you use /posttype/customtaxterm
}
add_action(\'init\',\'url_rewrites_wpanswers\');
您必须为每个站点添加一行,否则您的站点将无法运行。com/something1/something2最终会指向:

您的站点。com/索引。php?post\\U类型=something1&;自定义分类名称=something2

Keep in mind, with the functionality that you are requesting there can be conflicts with your permalinks if they are set to /post-type/post-name

您可以在此处阅读和了解更多信息:http://codex.wordpress.org/Rewrite_API/add_rewrite_rule

结束

相关推荐