Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Can you please let me know what is the best choice to create a Custom Post type like "Pen" which has Taxonomy called "Brand" now each of these post has definitely Black color which I will use as Custom Post Meta but some of Pen has more than 3 or 4 colors.Now I am kind of confuse to how to do that? Besides I need to print out the available color of the pan(if available) and link to them.

Update:

<?php
// Register Custom Post Type
function custom_post_type_pens() {
$labels = array(
    'name'                => 'Pens',
    'singular_name'       => 'Pen',
    'menu_name'           => 'Pen',
    'parent_item_colon'   => 'Parent Product:',
    'all_items'           => 'All Pens',
    'view_item'           => 'View Product',
    'add_new_item'        => 'Add New Pen',
    'add_new'             => 'Add Pen',
    'edit_item'           => 'Edit Pen',
    'update_item'         => 'Update Product',
    'search_items'        => 'Search Pens',
    'not_found'           => 'No Pen Found',
    'not_found_in_trash'  => 'No products found in Trash',
);
$args = array(
    'label'               => 'Pens',
    'description'         => 'Product information pages',
    'labels'              => $labels,
    'supports'            => array( ),
    'hierarchical'        => false,
    'public'              => true,
    'show_ui'             => true,
    'show_in_menu'        => true,
    'show_in_nav_menus'   => true,
    'show_in_admin_bar'   => true,
    'menu_position'       => 5,
    'menu_icon'           => '',
    'can_export'          => true,
    'has_archive'         => true,
    'exclude_from_search' => false,
    'publicly_queryable'  => true,
    'capability_type'     => 'page',
);
register_post_type( 'pens', $args );

}

// Hook into the 'init' action
add_action( 'init', 'custom_post_type_penss', 0 );

// Register Custom Taxonomy
function custom_taxonomy_pen()  {

    $labels = array(
        'name'                       => 'Brands',
        'singular_name'              => 'Brand',
        'menu_name'                  => 'Brands',
        'all_items'                  => 'All Brands',
        'parent_item'                => 'Parent Brand',
        'parent_item_colon'          => 'Parent Brands:',
        'new_item_name'              => 'New Brand Name',
        'add_new_item'               => 'Add New Brand',
        'edit_item'                  => 'Edit Brands',
        'update_item'                => 'Update Brand',
        'separate_items_with_commas' => 'Separate Brands with commas',
        'search_items'               => 'Search Brands',
        'add_or_remove_items'        => 'Add or Remove Brand',
        'choose_from_most_used'      => 'Choose From the Most Used Brands',
    );
    $args = array(
        'labels'                     => $labels,
        'hierarchical'               => true,
        'public'                     => true,
        'show_ui'                    => true,
        'show_admin_column'          => true,
        'show_in_nav_menus'          => true,
        'show_tagcloud'              => true,
    );
    register_taxonomy( 'brand', 'pens', $args );

}
  // Hook into the 'init' action
add_action( 'init', 'custom_taxonomy_pen', 0 );

add_action( 'add_meta_boxes', 'cd_meta_box_add' );  
function cd_meta_box_add()  
{  
    add_meta_box( 'my-meta-box-id', 'Pen Details', 'cd_meta_box_cb', 'pens', 'normal', 'high' );  
}  

function cd_meta_box_cb()  
{  
    ?>  
    <label for="my_meta_box_text">Price</label>  
    <input type="text" name="my_meta_box_text" id="my_meta_box_text" />  
    <label for="my_meta_box_text">Sale On</label>  
    <input type="checkbox" name="my_meta_box_text" id="my_meta_box_text" />  
    <label for="my_meta_box_text">Sale Price</label>  
    <input type="text" name="my_meta_box_text" id="my_meta_box_text" /> 
    <label for="my_meta_box_text">Color Optins</label>  
    <input type="checkbox" name="my_meta_box_text" id="my_meta_box_text" />  
<br />
  <?php 
      // $post is already set, and contains an object: the WordPress post  
    global $post;  
    $values = get_post_custom( $post->ID );  
    $text = isset( $values['my_meta_box_text'] ) ? $values['my_meta_box_text'] : '';  
    $selected = isset( $values['my_meta_box_select'] ) ? esc_attr( $values['my_meta_box_select'] ) : '';  
    $check = isset( $values['my_meta_box_check'] ) ? esc_attr( $values['my_meta_box_check'] ) : '';  

    // We'll use this nonce field later on when saving.  
    wp_nonce_field( 'my_meta_box_nonce', 'meta_box_nonce' ); 
    ?> 
    <p> 
        <label for="my_meta_box_text">Text Label</label> 
        <input type="text" name="my_meta_box_text" id="my_meta_box_text" value="<?php echo $text; ?>" /> 
    </p> 

    <p> 
        <label for="my_meta_box_select">Color</label> 
        <select name="my_meta_box_select" id="my_meta_box_select"> 
            <option value="red" <?php selected( $selected, 'red' ); ?>>Red</option> 
            <option value="blue" <?php selected( $selected, 'blue' ); ?>>Blue</option> 
        </select> 
    </p> 
    <p>
    <ul>
    <li class="front-page-element" id="front-page-element-placeholder">  
    <label for="element-page-id">Featured post:</label>  
       <label for="my_meta_box_select">Color</label> 
        <select name="my_meta_box_select" id="my_meta_box_select"> 
            <option value="red" <?php selected( $selected, 'red' ); ?>>Red</option> 
            <option value="blue" <?php selected( $selected, 'blue' ); ?>>Blue</option> 
        </select> 
    <a href="#">Remove</a>  
</li>  
    </ul>



    <ul class="Test">
    </ul>
    </p>
     <p> 
        <input id="adder" type="button" class="button-primary" value="Add More Color Options"></button>
    </p> 


    <p> 
        <input type="checkbox" id="my_meta_box_check" name="my_meta_box_check" <?php checked( $check, 'on' ); ?> />  
        <label for="my_meta_box_check">Do not check this</label>  
    </p>  
    <?php      

} 
?> 

<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script type="text/javascript">  
       jQuery(document).ready(function() {             
        jQuery('#adder').click(function() {  

          jQuery('.test').append('<li> <label for="my_meta_box_select">Color</label> <select name="my_meta_box_select" id="my_meta_box_select"> <option value="red" <?php selected( $selected, 'red' ); ?>>Red</option>           <option value="blue" <?php selected( $selected, 'blue' ); ?>>Blue</option>        </select> </li>');

        });           
    });  
</script>  
share|improve this question
1  
You can add as many color meta_key/meta-value pairs as you want. What have you tried? What exactly is causing you trouble? –  s_ha_dum Aug 22 '13 at 21:38
    
To be honest I am not sure on what color and how many! they will be! Is there any way to add custom meta dynamically as it is required? I already tried this out by appending elemnt using jquery on the Meta Box but as soon as i try to refresh the page they disapper –  Mona Coder Aug 22 '13 at 21:42
    
@ s_ha_dum , Can you please take a look at update? I added the jquey which I use to add more color options –  Mona Coder Aug 22 '13 at 21:45
    
What about using another custom taxonomy for these colors? –  Michal Mau Aug 22 '13 at 21:47
1  
Why are you building your form with Javascript? There is almost certainly information missing in the question. Please add as much detail as you can. I don't know what you are doing, or how, or why. –  s_ha_dum Aug 22 '13 at 21:48
show 3 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.