Viewing 3 reply threads
  • Author
    Posts
    • #4247
      redywebs
      Member

      Hi, I’m new to Piklist and I’ve made a hierarchical taxonomy for a custom post type, but want editors to only be able to select child terms. Can this be done with default metabox created or do I have to make a custom one and how?

      I’m also trying to create a shortcode or a widget to display on front-end a list of that taxonomy.

      Here is what I have

       $genreslabels = array(
          'name'              => __( 'Genres', 'redywebs' ),
          'singular_name'     => __( 'Genre', 'redywebs' ),
          'search_items'      => __( 'Search Genres', 'redywebs' ),
          'all_items'         => __( 'All Genres', 'redywebs' ),
          'parent_item'       => __( 'Parent Genre', 'redywebs' ),
          'parent_item_colon' => __( 'Parent Genre:', 'redywebs' ),
          'edit_item'         => __( 'Edit Genre', 'redywebs' ),
          'update_item'       => __( 'Update Genre', 'redywebs' ),
          'add_new_item'      => __( 'Add New Genre', 'redywebs' ),
          'new_item_name'     => __( 'New Genre Name', 'redywebs' ),
          'not_found'         => __( 'No Genres found.', 'redywebs' ),
          'menu_name'         => __( 'Genres', 'redywebs' ),
        );
      
          $taxonomies[] = array(
            'post_type' => 'catalog'
            ,'name' => 'colections'
            ,'configuration' => array(
              'hierarchical' => true
              ,'labels' => $genreslabels
              ,'show_ui' => true
              ,'query_var' => true
              ,'rewrite' => array( 
                'slug' => __('colection'),
              )
              ,'show_admin_column' => true
              ,'comments' => false
            )
          );

      And this code would show a checkbox list with parents disable, but I’m not sure how and where it should go to achieve what I want:

      $parents = get_terms('colections', array('hide_empty' => false,'parent' => 0 ));
          foreach ($parents as $parent => $value) {
           
            piklist('field', array(
              'type' => 'checkbox',
              'scope' => 'taxonomy',
              'field' => 'colections',
              'label' => $value->name,
              'choices' => piklist(
                  get_terms('colections', array(
                    'hide_empty' => false,
                    'child_of' => $value->term_id
                  )),
                  array(
                    'term_id',
                    'name'
                  )
              )
            ));
          }

      Could you please help me?

    • #4248
      Steve
      Keymaster

      @redywebs– Welcome to the Piklist community!

      One of the best parts of Piklist is that it’s code-based, which allows you to do almost anything.

      If you only want Editors (or above) to select child terms than you can wrap the field in a conditional statement:

      if(current_user_can('edit_pages')) :
      
      $parents = get_terms('colections', array('hide_empty' => false,'parent' => 0 ));
          foreach ($parents as $parent => $value) {
           
            piklist('field', array(
              'type' => 'checkbox',
              'scope' => 'taxonomy',
              'field' => 'colections',
              'label' => $value->name,
              'choices' => piklist(
                  get_terms('colections', array(
                    'hide_empty' => false,
                    'child_of' => $value->term_id
                  )),
                  array(
                    'term_id',
                    'name'
                  )
              )
            ));
          }
      
      endif;
      

      Let me know if that works for you.

    • #4250
      redywebs
      Member

      Hi Steve, thanks for your quick replay and your time.
      Sorry, I probably did not make myself clear enough (English is not my mother tongue). It is not a conditional statement I was after, I meant anyone editing the post. I have the first part of the code in my main plugin php file, but if I add the second part just after it, it does nothing. That’s why I asked. Should I make it a custom metabox, take it from there and put the whole thing in meta-boxes folder? What am I doing wrong?

      Next, I’d like to create a shortcode or a widget to display on front-end a list of that taxonomy. I have tried to follow all tutorials I’ve found, with no luck. Could you please give me some directions on how to do it?

      Thanks again for your time and patience.

    • #4251
      Steve
      Keymaster

      I think I understand now:
      1) Your Piklist Field ‘collections’ should go in the meta-boxes folder.
      2) Building widgets with Piklist is easy. You can follow this doc.

      Let me know if this helps.

Viewing 3 reply threads
  • You must be logged in to reply to this topic.