1
votes

I am currently developing my own travel website/blog. I would like to add "hotels" and "tips and tricks" to this website. I have made two custom post types that use the default post categories as a taxonomy (as shown below). I haven't bothered making custom taxonomies, as it would triple my work load, since I would just have to copy all the data from the default categories.

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')
    ));

Now, there are two things that I can't seem to achieve.

  1. Get the link of a category (for example: Mexico) that only shows a custom post type and not my default posts. (e.g. I would want to see the hotels in Mexico)
  2. Get an option in the admin-section (menu) that allows me to add said link to the menu.

Any help would be greatly appreciated.

1

1 Answers

0
votes

Try this

Create Custom Post :

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');

And for showing only a custom post type and not my default posts. You can customize $wp_query on your category.php template.

Like this :

$wp_query->query['post_type']='hotel';