问题描述
我正在使用 WC vendors 并根据他们的建议使用 wp_term_checklist 在产品上输出名为“location”的分类法。我已将其保存到产品中,但并未保存前端的复选框选择。 这是我添加到 product-edit.PHP 模板中的代码
$args = array(
'descendants_and_self' => 0,'selected_cats' => false,'popular_cats' => false,'walker' => null,'taxonomy' => 'location','checked_ontop' => false
);
wp_terms_checklist( $my_postid,$args );
$post_to_edit = array(
'ID' => $my_postid,'tax_input' => array( 'location' => array($_POST['tax_input']['location']) )
);
$pid = wp_update_post($post_to_edit);
if ( isset($_POST['tax_input']['location']) && is_array( $_POST['tax_input']['location'] ) ) {
$location = array_map( 'intval',$_POST['tax_input']['location'] );
$location = array_unique( $location );
wp_set_post_terms($pid,$location,'location');
}
这是他们使用多选的代码,但我们需要复选框: https://gist.github.com/digitalchild/128033d2d41f682acd4387b595d4f607
解决方法
我们的表单助手中有一个名为 wcv_terms_checklist() 的自定义术语清单。它不支持自定义分类法,但我现在对其进行了修改以支持它。您可以使用 v1.7.10 及更高版本中的以下代码在我们的前端获取工作自定义术语清单。
// Output the location checklist
function form_location( $object_id ) {
$args = array(
'taxonomy' => 'location',);
$field = array(
'id' => 'product_loc_list','label' => __( 'Location','wcvendors-pro' ),'class' => 'product_cat_checklist',);
WCVendors_Pro_Form_Helper::wcv_terms_checklist( $object_id,$args,$field );
}
add_action( 'wcv_after_product_details','form_location' );
// Save the terms
function save_location( $post_id ){
if ( isset( $_POST[ 'location' ] ) && is_array( $_POST[ 'location' ] ) ) {
$location = array_map( 'intval',$_POST[ 'location' ] );
$location = array_unique( $location );
wp_set_post_terms( $post_id,$location,'location' );
}
}
add_action( 'wcv_save_product','save_location' );
此处提供的要点 - https://gist.github.com/digitalchild/09e15649425845fef0b8d3af75c79dd1