自定义帖子类型类别链接+添加到菜单

时间:2018-01-23 作者:Michiel Standaert

我目前正在开发自己的旅游网站/博客。我想在这个网站上添加“酒店”和“提示和窍门”。我制作了两种自定义的帖子类型,它们使用默认的帖子类别作为分类法(如下所示)。我没有费心制作自定义分类法,因为这将使我的工作量增加三倍,因为我只需复制默认类别中的所有数据。

register_post_type(\'hotels\', 
        array(  \'taxonomies\'            => array(\'category\'),
                \'labels\'                => array(
                    \'name\'                  => __(\'Hotels\'),
                    \'singular_name\'         => __(\'Hotel\'),
                    \'add_new\'               => __(\'Add new hotel\'),
                    \'edit_item\'             => __(\'Edit hotel\'),
                    \'new_item\'              => __(\'New hotel\'),
                    \'view_item\'             => __(\'View hotel\'),
                    \'search_items\'          => __(\'Search hotels\'),
                    \'not_found\'             => __(\'No hotels found\'),
                    \'not_found_in_trash\'    => __(\'No hotels found in trash\')
                ),

                \'has_archive\'           => true,
                \'hierarchical\'          => true,
                \'public\'                => true,
                \'supports\'              => array(\'title\', \'editor\', \'post-formats\')
    ));
现在,有两件事我似乎无法实现。

获取仅显示自定义帖子类型而非我的默认帖子的类别链接(例如:墨西哥)。(例如,我想看看墨西哥的酒店)

  • 在管理部分(菜单)中获得一个选项,允许我将所述链接添加到菜单
  • 1 个回复
    SO网友:Dilip Gupta

    试试这个,我相信这会让你category 菜单中的分类法和custom post type.

    function custom_post_type() {
    
        $labels = array(
            \'name\'                  => _x( \'Post Types\', \'Post Type General Name\', \'text_domain\' ),
            \'singular_name\'         => _x( \'hotel\', \'Post Type Singular Name\', \'text_domain\' ),
            \'menu_name\'             => __( \'Post Types\', \'text_domain\' ),
            \'name_admin_bar\'        => __( \'Post Type\', \'text_domain\' ),
            \'archives\'              => __( \'Item Archives\', \'text_domain\' ),
            \'attributes\'            => __( \'Item Attributes\', \'text_domain\' ),
            \'parent_item_colon\'     => __( \'Parent Item:\', \'text_domain\' ),
            \'all_items\'             => __( \'All Items\', \'text_domain\' ),
            \'add_new_item\'          => __( \'Add New Item\', \'text_domain\' ),
            \'add_new\'               => __( \'Add New\', \'text_domain\' ),
            \'new_item\'              => __( \'New Item\', \'text_domain\' ),
            \'edit_item\'             => __( \'Edit Item\', \'text_domain\' ),
            \'update_item\'           => __( \'Update Item\', \'text_domain\' ),
            \'view_item\'             => __( \'View Item\', \'text_domain\' ),
            \'view_items\'            => __( \'View Items\', \'text_domain\' ),
            \'search_items\'          => __( \'Search Item\', \'text_domain\' ),
            \'not_found\'             => __( \'Not found\', \'text_domain\' ),
            \'not_found_in_trash\'    => __( \'Not found in Trash\', \'text_domain\' ),
            \'featured_image\'        => __( \'Featured Image\', \'text_domain\' ),
            \'set_featured_image\'    => __( \'Set featured image\', \'text_domain\' ),
            \'remove_featured_image\' => __( \'Remove featured image\', \'text_domain\' ),
            \'use_featured_image\'    => __( \'Use as featured image\', \'text_domain\' ),
            \'insert_into_item\'      => __( \'Insert into item\', \'text_domain\' ),
            \'uploaded_to_this_item\' => __( \'Uploaded to this item\', \'text_domain\' ),
            \'items_list\'            => __( \'Items list\', \'text_domain\' ),
            \'items_list_navigation\' => __( \'Items list navigation\', \'text_domain\' ),
            \'filter_items_list\'     => __( \'Filter items list\', \'text_domain\' ),
        );
        $args = array(
            \'label\'                 => __( \'hotel\', \'text_domain\' ),
            \'description\'           => __( \'Post Type Description\', \'text_domain\' ),
            \'labels\'                => $labels,
            \'supports\'              => array( \'title\', \'editor\', \'post-formats\' ),
            \'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,
            \'capability_type\'       => \'page\',
        );
        register_post_type( \'hotels\', $args );
    
    }
    add_action( \'init\', \'custom_post_type\', 0 );
    

    结束

    相关推荐

    Taxonomy Relationships

    我创建了两种分类法:制造车型这是针对CPT车辆的。不用说,品牌就是你的品牌(本田/丰田/福特等),然后你会根据品牌选择车型。如何设置Make和Model之间的关系?或者我在这方面完全错了吗?事实上,这应该是一个单一的分类法“Make&;型号\'?我不是在寻找:Vehicle CPT |-> Make |-> Model 我正在寻找:Vehicle CPT |-> Make |--> Model