【问题标题】:Wordpress: custom post type category link + add to menuWordpress:自定义帖子类型类别链接+添加到菜单
【发布时间】:2018-01-23 11:02:47
【问题描述】:

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

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. 在管理部分(菜单)中获取一个选项,允许我将所述链接添加到菜单中。

任何帮助将不胜感激。

【问题讨论】:

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


    【解决方案1】:

    试试这个

    创建自定义帖子:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-11-19
      • 2012-01-01
      • 2012-04-10
      • 1970-01-01
      • 2018-04-30
      • 1970-01-01
      • 2011-06-24
      • 1970-01-01
      相关资源
      最近更新 更多