简体   繁体   English

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

[英]Wordpress: custom post type category link + add to menu

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. 获取仅显示自定义帖子类型而不显示我的默认帖子的类别(例如:墨西哥)的链接。 (eg 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. 任何帮助将不胜感激。

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. 您可以在category.php模板上自定义$ wp_query。

Like this : 像这样 :

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM