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

时间:2018-01-23 11:02:48

标签: wordpress menu custom-post-type taxonomy custom-taxonomy

我目前正在开发自己的旅游网站/博客。我想添加"酒店"和#34;提示和技巧"到这个网站。我制作了两种自定义帖子类型,使用默认的帖子类别作为分类法(如下所示)。我没有打算制作自定义分类法,因为它会使我的工作负荷增加三倍,因为我只需要复制默认类别中的所有数据。

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. 获取仅显示自定义帖子类型而非我的默认帖子的类别(例如:墨西哥)的链接。 (例如,我希望看到墨西哥的酒店)
  2. 在admin-section(菜单)中获取一个选项,允许我将所述链接添加到菜单中。
  3. 非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

试试这个

创建自定义帖子:

function create_custom_hotel_post_type() {
    // set up labels
    $labels = array(
        'name' => 'Hotels',
        'singular_name' => 'hotel',
        'add_new' => 'Add New hotel',
        'add_new_item' => 'Add New hotel',
        'edit_item' => 'Edit hotel',
        'new_item' => 'New hotel',
        'all_items' => 'All hotel',
        'view_item' => 'View hotel',
        'search_items' => 'Search hotels',
        'not_found' => 'No hotels Found',
        'not_found_in_trash' => 'No hotels found in Trash',
        'parent_item_colon' => '',
        'menu_name' => 'hotels',
    );
    //register post type
    register_post_type('hotel', array(
        'labels' => $labels,
        'has_archive' => true,
        'public' => true,
        'supports' => array('title', 'editor', 'excerpt', 'custom-fields', 'thumbnail', 'page-attributes'),    
        'exclude_from_search' => false,
        'capability_type' => 'post',
        'rewrite' => array('slug' => 'hotel'),
        'menu_position'=> 7,
        'menu_icon'=> 'dashicons-building',
        'hierarchical' => true,
        'taxonomies'=> array('category'),
        )
    );
}

add_action('init', 'create_custom_hotel_post_type');

仅显示自定义帖子类型,而不是我的默认帖子。您可以在category.php模板上自定义$ wp_query。

像这样:

$wp_query->query['post_type']='hotel';
相关问题