自定义帖子类型和分类的URL段构成帖子/页面404

时间:2017-08-10 作者:Nicolas Telmaut

这个问题已经困扰了我好几天了,我觉得我已经接近了!

创建评论网站时,我有一个自定义的帖子类型,名为review, 按自定义层次分类法组织niche. 简而言之,我的最终目标是让我的URL看起来像这样:


mysite.com/coffee-maker/: 显示小众页面模板
mysite.com/coffee-maker/keurig-k425: 显示个人review

我尝试了这里提供的解决方案:http://wordpress.stackexchange.com/a/5313/1685, 这导致了两个问题:

CPT工作正常,但我的所有页面和帖子都返回404个错误。看着重写分析器,我意识到它们被路由到“利基:”规则。我意识到可能会有冲突,但不知道该怎么办

  • 页面内分类链接,如使用get_category_link() 仍然附加/niche/ URL的前缀,也就是404

    我的代码块:

    function cptui_register_my_cpts_review() {
    
        /**
         * Post Type: Reviews.
         */
    
        $labels = array(
            "name" => __( "Reviews", "" ),
            "singular_name" => __( "Review", "" ),
            "menu_name" => __( "Mes Reviews", "" ),
            "all_items" => __( "Toutes les Reviews", "" ),
            "add_new" => __( "Ajouter une Review", "" ),
            "add_new_item" => __( "Ajouter une Review", "" ),
            "edit_item" => __( "Modifier la Review", "" ),
            "new_item" => __( "Nouvelle Review", "" ),
            "view_item" => __( "Voir la Review", "" ),
            "view_items" => __( "Voir les Reviews", "" ),
            "search_items" => __( "Rechercher une Review", "" ),
            "not_found" => __( "Pas de Review Trouvée", "" ),
            "not_found_in_trash" => __( "Pas de Review Trouvée dans Corbeille", "" ),
            "parent_item_colon" => __( "Review Parente", "" ),
            "insert_into_item" => __( "Insérer dans la Review", "" ),
            "uploaded_to_this_item" => __( "Insérer dans la Review", "" ),
            "filter_items_list" => __( "Filtrer la Liste des Reviews", "" ),
            "items_list_navigation" => __( "Navigation dans la Liste des Reviews", "" ),
            "items_list" => __( "Liste des Reviews", "" ),
            "attributes" => __( "Attributs de la Review", "" ),
            "parent_item_colon" => __( "Review Parente", "" ),
        );
    
        $args = array(
            "label" => __( "Reviews", "" ),
            "labels" => $labels,
            "description" => "",
            "public" => true,
            "publicly_queryable" => true,
            "show_ui" => true,
            "show_in_rest" => false,
            "rest_base" => "",
            "has_archive" => false,
            "show_in_menu" => true,
            "exclude_from_search" => false,
            "capability_type" => "post",
            "map_meta_cap" => true,
            "hierarchical" => false,
            "rewrite" => array( "slug" => "%niche%", "with_front" => true ),
            "query_var" => false,
            "menu_icon" => "dashicons-awards",
            "supports" => array( "title", "editor", "thumbnail", "excerpt", "custom-fields", "genesis-seo" ),
            "taxonomies" => array( "niche" ),
        );
    
        register_post_type( "review", $args );
    }
    
    add_action( \'init\', \'cptui_register_my_cpts_review\' );
    
    
    function cptui_register_my_taxes_niche() {
    
        /**
         * Taxonomy: Niches.
         */
    
        $labels = array(
            "name" => __( "Niches", "" ),
            "singular_name" => __( "Niche", "" ),
            "menu_name" => __( "Niches", "" ),
            "all_items" => __( "Toutes les niches", "" ),
            "edit_item" => __( "Modifier la niche", "" ),
            "view_item" => __( "Voir la niche", "" ),
            "update_item" => __( "Mettre à jour le nom de la niche", "" ),
            "add_new_item" => __( "Ajouter nouvelle niche", "" ),
            "new_item_name" => __( "Nom de la nouvelle niche", "" ),
            "parent_item" => __( "Niche parente", "" ),
            "parent_item_colon" => __( "Niche parente :", "" ),
            "search_items" => __( "Rechercher les niches", "" ),
            "popular_items" => __( "Niches Populaires", "" ),
            "separate_items_with_commas" => __( "Séparer les niches avec des virgules", "" ),
            "add_or_remove_items" => __( "Ajouter ou enlever des Niches", "" ),
            "choose_from_most_used" => __( "Choisir parmi les plus utilisées", "" ),
            "not_found" => __( "Pas trouvé !", "" ),
            "no_terms" => __( "Pas de niches", "" ),
            "items_list_navigation" => __( "Navigation par liste des niches", "" ),
            "items_list" => __( "Liste des niches", "" ),
        );
    
        $args = array(
            "label" => __( "Niches", "" ),
            "labels" => $labels,
            "public" => true,
            "hierarchical" => true,
            "label" => "Niches",
            "show_ui" => true,
            "show_in_menu" => true,
            "show_in_nav_menus" => true,
            "query_var" => true,
            "rewrite" => array( \'slug\' => \'niche\', \'with_front\' => false, \'hierarchical\' => false ),
            "show_admin_column" => false,
            "show_in_rest" => false,
            "rest_base" => "",
            "show_in_quick_edit" => true,
        );
        register_taxonomy( "niche", array( "review" ), $args );
    }
    
    add_action( \'init\', \'cptui_register_my_taxes_niche\' );
    
    function wpse_5308_post_type_link( $link, $post ) {
    
        if ( $post->post_type === \'review\' ) {
            if ( $terms = get_the_terms( $post->ID, \'niche\' ) )
                $link = str_replace( \'%niche%\', current( $terms )->slug, $link );
        }
    
        return $link;
    }
    
    add_filter( \'post_type_link\', \'wpse_5308_post_type_link\', 10, 2 );
    

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

    这一次过去让我绊倒了。如果分类法和post类型重写之间存在冲突,那么在post类型之前注册分类法是很重要的。

    假设您可以将您的评论URL修改为格式/评论/%利基%/%postname%,那么您需要更改您的帖子类型,如下所示:

    \'rewrite\' => array( \'slug\' => \'reviews/%niche%\', \'with_front\' => false ),
    \'has_archive\' => \'reviews\',
    
    您的分类:

    \'rewrite\' => array( \'slug\' => \'reviews\', \'with_front\' => false ),
    
    如上所述,请致电register_taxonomy() 在您致电之前register_post_type(). 访问设置>永久链接并重新保存以刷新重写规则。

    结束

    相关推荐

    Ordering posts by an array

    我有一个查询,它返回许多不同的自定义帖子类型。我想按帖子类型数组的内容对帖子数组进行排序;e、 g。array(\'post\', \'video\', \'testimonial\'..... );数组(顺序)是固定的。如何在不对每种职位类型进行不同查询的情况下管理此问题?