【问题标题】:WC Vendors - Custom Taxonomy Checklist Not Saving on Front-EndWC 供应商 - 自定义分类清单未保存在前端
【发布时间】:2021-07-23 14:20:23
【问题描述】:

我正在使用 WC 供应商,并根据他们的建议使用 wp_term_checklist 在产品上输出名为“位置”的分类法。我已经将它保存到产品中,但它没有保存前端的复选框选择。 这是我添加到 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

【问题讨论】:

    标签: wordpress taxonomy wcvendors


    【解决方案1】:

    我们的表单助手中有一个名为 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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-02-06
      • 2013-04-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-08
      • 2012-04-24
      相关资源
      最近更新 更多