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,       
     )    
    );  
    }
  • 3
    Just make the 'hierarchical' => FALSE, to true. :) – Mayeenul Islam Oct 19 '15 at 12:54
  • so the only thing i have to do is chance 'hierarchical' => FALSE, in 'hierarchical' => true, ?? – tom84 Oct 19 '15 at 14:26
  • *chance = change" – tom84 Oct 19 '15 at 15:10
0

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.

  • thankyou... i cant understand this exactly. but do you mean i need to modifiy this: 'supports' => $support, What must i do to see he select box in the editor page? – tom84 Oct 19 '15 at 14:29
  • @tom84 See my added notes about both $supports and $args – phatskat Oct 19 '15 at 14:36
  • ok thankyou very much!, i will try it! – tom84 Oct 19 '15 at 14:58
  • it worked fine! Thankyou very much! – tom84 Oct 20 '15 at 18:19
  • @tom84 glad it worked! please accept my answer if it did the trick – phatskat Oct 22 '15 at 14:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.