【问题标题】:How to add acf form post categry field in acf form front end but category dropdown not showing如何在acf表单前端添加acf表单帖子类别字段但类别下拉列表未显示
【发布时间】:2017-05-22 12:11:57
【问题描述】:

类别下拉菜单未显示在前端。请提出建议或让我知道我的代码哪里错了。

   <?php  acf_form(array(
                'post_id'       => 'new_post',
                'post_title'    => true,
                'post_content'  => true,
                'post_category' => true,
                'field_groups' => array('group_57d9928ba5858'),
                'new_post'      => array(
                    'post_type'     => 'festival',
                    'post_status'   => 'draft'
                ),
                'submit_value'       => 'Submit Post',
                'updated_message'    => 'Saved!',
                    'uploader'           => 'wp',
            ));?>

【问题讨论】:

    标签: php wordpress advanced-custom-fields


    【解决方案1】:

    我在https://www.advancedcustomfields.com/resources/acf_form/ 中看不到 post_category 作为选项,我认为它应该在

    'new_post' => array(
         'post_type' => 'foo',
         'post_status' => 'publish',
         'post_category' => array ('bar')
    ),
    

    【讨论】:

      【解决方案2】:

      ACF 不会自动将类别选项添加到前端表单。最好的解决方案是设置一个引用类别分类法(Taxonomy Field Type)的自定义字段,并将 Add Term、Save Term 和 Load Term 值设置为 true,这样该字段就像一个原生类别字段一样。

      或者,您可以将以下代码添加到从 ACF 生成 PHP 工具为我使用这些设置设置的字段生成的函数文件中。

      
      if( function_exists('acf_add_local_field_group') ) {
      acf_add_local_field_group(array(
          'key' => 'group_5dc382838casdf',
          'title' => 'Category',
          'fields' => array(
                  array(
                  'key' => 'field_5ddcdd3232asD',
                  'label' => 'Category',
                  'name' => 'category',
                  'type' => 'taxonomy',
                  'instructions' => '',
                  'required' => 1,
                  'conditional_logic' => 0,
                  'wrapper' => array(
                      'width' => '',
                      'class' => '',
                      'id' => '',
                  ),
                  'taxonomy' => 'category',
      
                  // Accepts SINGLE VALUE: radio, select, MULTIPLE VALUES: multi_select, checkbox
                  'field_type' => 'multi-select', 
                  'allow_null' => 0,
      
                  // Ensures taxonomy relationships are set on add, save, load
                  'add_term' => 1,
                  'save_terms' => 1,
                  'load_terms' => 1,
      
                  'return_format' => 'id',
                  'multiple' => 0,
      
                  // Form Placeholder
                  'placeholder' => 'Select Topics'
              )
          ),
          'location' => array(
              array(
                  array(
                      'param' => 'post_type',
                      'operator' => '==',
      
                      // ADD YOUR POST-TYPE HERE
                      'value' => 'post',
                  ),
              ),
          ),
          'menu_order' => 0,
          'position' => 'normal',
          'style' => 'default',
          'label_placement' => 'top',
          'instruction_placement' => 'label',
          'hide_on_screen' => '',
          'active' => true,
          'description' => '',
      ));    
      }
      

      【讨论】:

        【解决方案3】:

        这是有效的...

        function acf_xyz_categories( $value, $post_id, $field  ){ 
            if($value != ''){
                $valueint = array_map('intval', $value);
        $set_taxonomy_ids = wp_set_object_terms( $post_id, $valueint, 'your_taxonomy_name', true );
            }
            return $value;
        }
        add_filter('acf/update_value/name=abc_category', 'acf_xyz_categories', 10, 3);
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-12-11
          • 1970-01-01
          • 2021-11-28
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多