添加时自动添加类别 post

Add category automatically while adding post

我正在为一个新闻网站使用 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" 中。我不知道要编辑哪个文件。

您需要使用 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');