i have wordpress project used cms. wonderful community, i've gone knowing nothing wordpress being able create custom post types (for product list), categorize them using hierarchical taxonomies, , create custom fields product post type.
now, products pretty simple. have productname
, producttype
, productcategory
, documentname
, , documenturl
. last 2 related because documenturl
link pdf on web, , documentname
label documenturl. i've created custom fields documentname
, documenturl
, can add 1 of each each product
custom post. however, product
have many document url's , document names, or have 1, or 0. there way make dynamic doesn't matter how many have? or need come max number , create many custom fields product custom post?
if straight php or asp.net create seperate db table document elements , throw nice form jquery started 2 or 3 document fields, , if needed more click +
symbol add row of document fields (like http://deepliquid.com/projects/appendo/demos.php). loop through them , add them database. possible wordpress?
the other idea had creating new custom post type documents , creating relationship products, can't wrap head around how work.
any advice appreciated! thank you!
i'm gonna assume comfortable php given you've said here. news php knowledge come in handy here. it's time learn new feature of register_post_type function; is, register_meta_box_cb
parameter. allows define function called add metabox on cpt add , edit pages. instance, here cpt (taken straight codex) added parameter:
add_action('init', 'codex_custom_init'); function codex_custom_init() { $labels = array( 'name' => _x('books', 'post type general name'), 'singular_name' => _x('book', 'post type singular name'), 'add_new' => _x('add new', 'book'), 'add_new_item' => __('add new book'), 'edit_item' => __('edit book'), 'new_item' => __('new book'), 'view_item' => __('view book'), 'search_items' => __('search books'), 'not_found' => __('no books found'), 'not_found_in_trash' => __('no books found in trash'), 'parent_item_colon' => '', 'menu_name' => 'books' ); $args = array( 'labels' => $labels, 'public' => true, 'publicly_queryable' => true, 'show_ui' => true, 'show_in_menu' => true, 'query_var' => true, 'rewrite' => true, 'capability_type' => 'post', 'has_archive' => true, 'hierarchical' => false, 'menu_position' => null, 'register_meta_box_cb' => 'my_meta_box_function', 'supports' => array('title','editor','author','thumbnail','excerpt','comments') ); register_post_type('book',$args); }}
now have meta box cb function defined, use like:
function my_beta_box_function(){ add_meta_box( 'myplugin_sectionid', __( 'my post section title', 'myplugin_textdomain' ), 'myplugin_inner_custom_box', 'book' ); }
the add_meta_box defines meta box , provides function creates content within metabox. in case, our code referring function myplugin_inner_custom_box
. take @ http://codex.wordpress.org/function_reference/add_meta_box more on adding meta boxes. so, need add our content defining function:
function myplugin_inner_custom_box() { // here appear in meta box }
at point, can utilize php knowledge create html form can processed on post submit. can add + button , use php in other php application. won't go how because i'll assume can it. i've create few metaboxes , after knowing functions posted, rely on php knowledge. last piece of saving input. using function defines save routine , save_post action, can use php knowledge save data:
/* data entered */ add_action('save_post', 'myplugin_save_postdata'); /* when post saved, saves our custom data */ function myplugin_save_postdata( $post_id ) { // verify if auto save routine. // if our form has not been submitted, dont want if ( defined('doing_autosave') && doing_autosave ) return $post_id; // verify came our screen , proper authorization, // because save_post can triggered @ other times if ( !wp_verify_nonce( $_post['myplugin_noncename'], plugin_basename(__file__) ) ) return $post_id; // check permissions if ( 'page' == $_post['post_type'] ) { if ( !current_user_can( 'edit_page', $post_id ) ) return $post_id; } else { if ( !current_user_can( 'edit_post', $post_id ) ) return $post_id; } // ok, we're authenticated: need find , save data $mydata = $_post['myplugin_new_field']; update_post_meta('my_field', $mydata); return $mydata; }
finally, note on save function. if name input fields array value (e.g., docs[]
), $_post['docs'] value array. fortunately, wp's update_post_meta
function setup handle array inputs. serialize , input it. using compatible get_post_meta
function unserialize array use ready data out.
good luck!
Comments
Post a Comment