将术语名称和术语父项放入自定义帖子类型固定链接

时间:2013-07-01 作者:PixelBBW

That i would like (for taxonomy page):

当前URL:myurl。com/商业/工匠/布切里/

我尝试做什么URL:myurl。com/商业/工匠/食品/布切里/

营养是Boucherie的父母。

That i would like (for single page) :

当前URL:myurl。com/商业/工匠/食品/单件

我尝试做什么URL:myurl。com/商业/工匠/食品/布切里/singlepost

营养是Boucherie的父母。

谢谢你的帮助。

这是我的代码:



//This code tells WordPress when to run our setup function
add_action(\'after_setup_theme\', \'map\');

function map(){

//setup slider post type
//Set the names of the labels for the Post type interface
 $post_labels = array(
        \'name\' => \'Carte\',
        \'singular_name\' => \'Carte\',
        \'add_new\' => \'Ajouter un point carte\',
        \'add_new_item\' => \'Ajouter un point carte\',
        \'edit_item\' => \'Editer un point carte\',
        \'new_item\' => \'Nouveau point carte\',
        \'view_item\' => \'Afficher un point carte\',
        \'search_items\' => \'Rechercher un point carte\',
        \'not_found\' =>  \'Aucun point carte\',
        \'not_found_in_trash\' => \'Aucun point carte trouvés dans la corbeille\',
        \'parent_item_colon\' => \'\'
    );


//Setup the rules for this post type. You can read more about what each of these do in
//the WP codex under register_post_type().
    $post_args = array(
        \'labels\' => $post_labels, //Add the labels that we set above
        \'public\' => true,
        \'publicly_queryable\' => true,//hide this post type from being accessed directly
        \'show_ui\' => true,
        \'show_in_menu\' => true,
        \'query_var\' => true,
        \'rewrite\' => array(\'slug\'=>\'commerce/artisans/%map_categories%\',\'with_front\'=>false),
        \'capability_type\' => \'post\',
        \'hierarchical\' => false,
        \'supports\' => array(\'title\',\'editor\' ), //specify what items the post type supports.
        \'has_archive\' => false,
        \'menu_icon\' => get_stylesheet_directory_uri() . \'/map.png\',
    );
    //register the post type using the arguments we have setup above.
    register_post_type(\'map\',$post_args);
    register_taxonomy( \'map_categories\', \'map\', array( \'hierarchical\' => true, \'label\' => \'Catégories\', \'query_var\' => true, \'rewrite\' => array(\'slug\'=>\'commerce/artisans\',\'with_front\'=>false) ) );

}

function map_categories_link( $post_link, $id = 0 ){
    $post = get_post($id);  
    if ( is_object( $post ) ){
        $terms = wp_get_object_terms( $post->ID, \'map_categories\' );
        if( $terms ){
            return str_replace( \'%map_categories%\' , $terms[0]->slug , $post_link );
        }
    }
    return $post_link;  
}
add_filter( \'post_type_link\', \'map_categories_link\', 1, 3 );

1 个回复
SO网友:Md Toufiqul Islam

我通过以下代码解决了这个问题,其中需要定义“重写”参数,如下所示

register_taxonomy( \'map_categories\', \'map\', array( \'hierarchical\' => true, \'label\' => \'Catégories\', \'query_var\' => true, \'rewrite\' => array(\'slug\'=>\'commerce/artisans\',\'with_front\'=>false, \'hierarchical\' => true) ) );
你需要理解重写中的“分层”实际上解决了你的目的。

更新代码后,需要再次保存永久链接结构。:)

结束

相关推荐

WordPress不显示wp-Content/Themes文件夹中的主题

我会尽我所能解释这一点,但至少可以说这很奇怪。因此,两个默认主题(210和2111)出现在wp admin的外观>主题选项卡下。但是,如果我向wp内容/主题添加一个新主题(即使只是一个空文件夹),则除了当前使用的主题之外,所有主题都无法显示在“外观”>“主题”选项卡上。有人有什么想法吗?--这是在LAMP服务器上(Ubuntu 12.04)。