添加帖子时自动添加类别

时间:2016-05-17 18:31:01

标签: php wordpress advanced-custom-fields

我在新闻网站上使用wordpress。 有一个名为Headlines的地方,有以下ACF代码。这会将帖子添加到网站的标题中。

array (
                        'key' => 'field_53e3e2fc67dc4',
                        'label' => 'Headlines',
                        'name' => 'hp_headlines',
                        'prefix' => '',
                        'type' => 'repeater',
                        'instructions' => '',
                        'required' => 0,
                        'conditional_logic' => 0,
                        'wrapper' => array (
                            'width' => '',
                            'class' => '',
                            'id' => '',
                        ),
                        'min' => '',
                        'max' => '',
                        'layout' => 'row',
                        'button_label' => 'Add Headline',
                        'sub_fields' => array (
                            array (
                                'key' => 'field_54621f720bfdc',
                                'label' => 'Headline Type',
                                'name' => 'hp_headline_type',
                                'prefix' => '',
                                'type' => 'radio',
                                'instructions' => '',
                                'required' => 1,
                                'conditional_logic' => 0,
                                'wrapper' => array (
                                    'width' => '',
                                    'class' => '',
                                    'id' => '',
                                ),
                                'choices' => array (
                                    'url' => 'URL',
                                    'article' => 'Article',
                                ),
                                'other_choice' => 0,
                                'save_other_choice' => 0,
                                'default_value' => 'url',
                                'layout' => 'horizontal',
                            ),
                            array (
                                'key' => 'field_54621fa20bfdd',
                                'label' => 'URL',
                                'name' => 'hp_headline_url',
                                'prefix' => '',
                                'type' => 'url',
                                'instructions' => '',
                                'required' => 1,
                                'conditional_logic' => array (
                                    array (
                                        array (
                                            'field' => 'field_54621f720bfdc',
                                            'operator' => '==',
                                            'value' => 'url',
                                        ),
                                    ),
                                ),
                                'wrapper' => array (
                                    'width' => '',
                                    'class' => '',
                                    'id' => '',
                                ),
                                'default_value' => '',
                                'placeholder' => 'http://',
                            ),
                            array (
                                'key' => 'field_53e3e34067dc5',
                                'label' => 'Article',
                                'name' => 'hp_headline_article',
                                'prefix' => '',
                                'type' => 'post_object',
                                'instructions' => '',
                                'required' => 1,
                                'conditional_logic' => array (
                                    array (
                                        array (
                                            'field' => 'field_54621f720bfdc',
                                            'operator' => '==',
                                            'value' => 'article',
                                        ),
                                    ),
                                ),
                                'wrapper' => array (
                                    'width' => '',
                                    'class' => '',
                                    'id' => '',
                                ),
                                'post_type' => array (
                                    0 => 'post',
                                ),
                                'taxonomy' => '',
                                'allow_null' => 0,
                                'multiple' => 0,
                                'return_format' => 'id',
                                'ui' => 1,
                            ),                               
                        ),

创建一个字段,将已添加的帖子添加到标题列表中。 现在我希望在按下更新按钮时将这些字段自动添加到“xyz”类别中。我不知道要编辑哪个文件。

enter image description here

1 个答案:

答案 0 :(得分:4)

你需要使用save_post动作,在你的functions.php中添加这个代码,用你的猫id改变猫ID

function set_my_categories($post_ID){
  if(wp_is_post_autosave($post_ID) || wp_is_post_revision($post_ID)) {
  return $post_ID;
  }
  wp_set_post_categories( $post_ID, array(49,13) );
  }
  add_action('save_post', 'set_my_categories');