最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Parent and Child relation for custom post types

programmeradmin0浏览0评论

i use this code for a custom post type calles portfolio. Now i want to use a parent-child realtion between the items. For example i have a portfolio item with this URL: www.domain.de/portfolio-item1

Another Portfolio Item should be a child from the portfolio item1 and the url should be
www.domain.de/portfolio-item1/portfolio-item2

Im a beginner with php and i dont know how i can realise this. Can somebody help me?

 // Portfolio Post Type

    add_action('init','wpthesis_create_portfolio_init');
    function wpthesis_create_portfolio_init()  {
        $labels = array    
    (        'name' => _x('Portfolio', 'post type general name'),
             'singular_name' => _x('item', 'post type singular name'),
             'add_new' => _x('Add New', 'Portfolio Item'), 
             'add_new_item' => __('Add New Portfolio Item'), 
             'edit_item' => __('Edit Portfolio Item'),  
            'new_item' => __('New Portfolio Item'),
            'view_item' => __('View Portfolio Item'),
            'search_items' => __('Search Portfolio'), 
            'not_found' =>  __('No portfolio items found'), 
            'not_found_in_trash' => __('No portfolio items found in Trash'), 
            'parent_item_colon' => ''    
    ); 

       $support = array    
    (       
     'title', 
           'editor',
            'author',
            'thumbnail',
            'custom-fields',
            'comments',
            'genesis-seo', 
            'genesis-layouts',
            'revisions'    
    );    

    $args = array  
      (     
       'labels' => $labels,
            'public' => TRUE,
            'rewrite' => array('slug'=>('produkte-leistungen'),'with_front'=>false),
            'capability_type' => 'page', 
           'hierarchical' => FALSE,
            'query_var' => true, 
           'supports' => $support,        'taxonomies' => array('portfolio-category'),
            'menu_position' => 5 
       );    
     register_post_type('portfolio',$args);

            register_taxonomy(
            'portfolio-category', 
            'portfolio',        

    array(            
    'hierarchical' => TRUE, 
               'label' => 'Categories',
                'query_var' => TRUE,
                'rewrite' => FALSE,       
     )    
    );  
    }

i use this code for a custom post type calles portfolio. Now i want to use a parent-child realtion between the items. For example i have a portfolio item with this URL: www.domain.de/portfolio-item1

Another Portfolio Item should be a child from the portfolio item1 and the url should be
www.domain.de/portfolio-item1/portfolio-item2

Im a beginner with php and i dont know how i can realise this. Can somebody help me?

 // Portfolio Post Type

    add_action('init','wpthesis_create_portfolio_init');
    function wpthesis_create_portfolio_init()  {
        $labels = array    
    (        'name' => _x('Portfolio', 'post type general name'),
             'singular_name' => _x('item', 'post type singular name'),
             'add_new' => _x('Add New', 'Portfolio Item'), 
             'add_new_item' => __('Add New Portfolio Item'), 
             'edit_item' => __('Edit Portfolio Item'),  
            'new_item' => __('New Portfolio Item'),
            'view_item' => __('View Portfolio Item'),
            'search_items' => __('Search Portfolio'), 
            'not_found' =>  __('No portfolio items found'), 
            'not_found_in_trash' => __('No portfolio items found in Trash'), 
            'parent_item_colon' => ''    
    ); 

       $support = array    
    (       
     'title', 
           'editor',
            'author',
            'thumbnail',
            'custom-fields',
            'comments',
            'genesis-seo', 
            'genesis-layouts',
            'revisions'    
    );    

    $args = array  
      (     
       'labels' => $labels,
            'public' => TRUE,
            'rewrite' => array('slug'=>('produkte-leistungen'),'with_front'=>false),
            'capability_type' => 'page', 
           'hierarchical' => FALSE,
            'query_var' => true, 
           'supports' => $support,        'taxonomies' => array('portfolio-category'),
            'menu_position' => 5 
       );    
     register_post_type('portfolio',$args);

            register_taxonomy(
            'portfolio-category', 
            'portfolio',        

    array(            
    'hierarchical' => TRUE, 
               'label' => 'Categories',
                'query_var' => TRUE,
                'rewrite' => FALSE,       
     )    
    );  
    }
Share Improve this question edited Oct 19, 2015 at 12:54 Mayeenul Islam 12.9k21 gold badges85 silver badges169 bronze badges asked Oct 19, 2015 at 12:52 tom84tom84 2551 gold badge6 silver badges21 bronze badges 3
  • 3 Just make the 'hierarchical' => FALSE, to true. :) – Mayeenul Islam Commented Oct 19, 2015 at 12:54
  • so the only thing i have to do is chance 'hierarchical' => FALSE, in 'hierarchical' => true, ?? – tom84 Commented Oct 19, 2015 at 14:26
  • *chance = change" – tom84 Commented Oct 19, 2015 at 15:10
Add a comment  | 

1 Answer 1

Reset to default 2

To elaborate on what Mayeenul Islam said, from the WP Docs on regsiter_post_type

hierarchical
(boolean) (optional) Whether the post type is hierarchical (e.g. page). Allows Parent to be specified. The 'supports' parameter should contain 'page-attributes' to show the parent select box on the editor page. Default: false

Emphasis mine. After setting that to true, you should be able to associate the posts with each other.

EDIT: Code added

$support = array (
    'title',
    'editor',
    'author',
    'thumbnail',
    'custom-fields',
    'comments',
    'genesis-seo',
    'genesis-layouts',
    'revisions',
    // Add this to supports
    'page-attributes',
);

$args = array  (
    'labels' => $labels,
    'public' => TRUE,
    'rewrite' => array('slug'=>('produkte-leistungen'),'with_front'=>false),
    'capability_type' => 'page',
    // You had this set to FALSE, try it with TRUE
    'hierarchical' => TRUE,
    'query_var' => true,
    'supports' => $support,
    'taxonomies' => array('portfolio-category'),
    'menu_position' => 5
);

register_post_type('portfolio',$args);

In the above snippet, I added 'page-attributes' to $support and set the 'hierarchical' option to TRUE in $args. That should get you set up properly.

发布评论

评论列表(0)

  1. 暂无评论