Php 添加帖子时自动添加类别

Php 添加帖子时自动添加类别,php,wordpress,advanced-custom-fields,Php,Wordpress,Advanced Custom Fields,我正在使用wordpress作为新闻网站。 有一个地方叫做标题,下面是ACF代码。这会在网站的标题中添加帖子 array ( 'key' => 'field_53e3e2fc67dc4', 'label' => 'Headlines', 'name' => 'hp_headlines',

我正在使用wordpress作为新闻网站。 有一个地方叫做标题,下面是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\u post操作,在函数中添加此代码。php使用您的cat ID更改cat 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');

您需要使用save_post操作,将此代码添加到函数中。php使用您的cat ID更改cat 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');

不清楚,你想实现什么,请多解释一点,如果可能的话添加屏幕截图,谢谢。我想用php代码链接一个类别和一篇文章。但我没有找到这样做的文件。上面的ACF用于创建字段,在提交字段后,我想将类别和上面字段中选择的帖子链接起来。你的意思是要将类别分配给帖子?这里选择了Hello world帖子,因此按下“保存选项”,我想将Hello world帖子放在下面。类别$cat。我没有找到可以应用wp_set_post_categories函数的PHP文件。不清楚,你想实现什么,请多解释一点,如果可能的话请添加屏幕截图。我想用php代码链接一个类别和一篇文章。但我没有找到这样做的文件。上面的ACF用于创建字段,在提交字段后,我想将类别和上面字段中选择的帖子链接起来。你的意思是要将类别分配给帖子?这里选择了Hello world帖子,因此按下“保存选项”,我想将Hello world帖子放在下面。类别$cat。我没有找到可以应用wp_set_post_categories函数的PHP文件。谢谢你的帮助。我会试试这个然后回来。谢谢你的帮助。我会试试这个然后回来。