在 WordPress 中根据 Post Object (ACF) 标题自动生成标签
Automatically generate tags based on Post Object (ACF) titles in WordPress
我正在为我们的项目页面创建自定义 post 类型。我还为我们的员工定制了 posts 类型。
在 ACF 中,我创建了一个关系字段,您可以在其中将团队成员添加到项目中,并显示在网站上。
基于关系字段中选定的团队成员 posts,我希望为关系字段中加载的每个职位(员工姓名)生成一个标签。
这是我现在卡住了。
PostObject中的名字叫做teamleden。我已尝试将代码添加到我的海关 posts 类型文件中,但它不起作用。
<?php
// save post action
add_action('acf/save_post', 'set_employee_tags_on_save_update', 20);
/**
* @param $post_id int|string
*/
function set_employee_tags_on_save_update($post_id) {
// get our current post object
$post = get_post($post_id);
// if post is object
if(is_object($post)) {
// check we are on the projects custom type and post statuses are either publish or draft
// change 'projects' post type to your post type name which has the relationship field
if($post->post_type === 'projects' && ($post->post_status === 'publish' || $post->post_status === 'draft')) {
// get relationship field employees
// this example uses Post Object as the Return Format and is a multiple value
// change get field param 'employees' to your relationship field name
$employees = get_field('employees');
// team member tags to set empty array
$team_member_tags_to_set = [];
// if employees has value or values
if($employees) {
// get all of our current team member tags
// change 'team_members' taxonomy value to your members taxonomy name
$team_member_tags = get_terms([
'taxonomy' => 'team_members',
'orderby' => 'name',
'order' => 'ASC'
]);
// empty array for existing team member tags
$existing_team_member_tags = [];
// if we have existing team member tags
if(!empty($team_member_tags)) {
// foreach team member tags as team member tag
foreach($team_member_tags as $team_member_tag) {
// add existing team member to our existing team member tags array by tag ID => tag name
// this is so we can use this later to check if a team member tag already exists so we dont create duplicates
$existing_team_member_tags[$team_member_tag->ID] = $team_member_tag->name;
}
}
// foreach employees as employee
foreach($employees as $employee) {
// get the title for current employee
$title = get_the_title($employee->ID);
// search existing team members array and return the tag id via key
$existing_team_member_tag_id = array_search($title, $existing_team_member_tags);
// if we have an existing team member tag id
if($existing_team_member_tag_id) {
// add the existing team member tag id as integer to array
$team_member_tags_to_set[] = (int)$existing_team_member_tag_id;
} else {
// else create a new tag for team member by adding title (name) as string to array
$team_member_tags_to_set[] = (string)$title;
}
}
}
// remove the action
remove_action('acf/save_post', 'acf_save_post');
// set post tags for this post id, removing any unused team member tags if relationship field team members are changed
wp_set_object_terms($post_id, $team_member_tags_to_set, $taxonomy = 'team_members', false);
// re add the action
add_action('acf/save_post', 'acf_save_post');
}
}
// finally return
return;
}
这还没有经过测试,但应该能让您走上正轨。
您需要将此添加到 function.php
。
参考您需要在我的示例代码中更改的内容...
projects
post_type 是 post 类型名称,此代码在保存或更新 post 时触发。
employees
是projects
post类型中acf关系字段的名称。
team_members
是您应该在 projects
post 类型上使用的自定义标签分类法名称。
基本上,这段代码的作用是在使用 acf/save_post
操作保存、发布或更新 projects
post 时。它获取此 post 的 acf 关系字段成员数据。如果字段中有成员,它会获取所有现有的团队成员标签,并通过 ID => Title(name) 创建一个简单的现有成员标签数组。
然后遍历关系字段中的所有成员,并检查成员的标签是否已经存在,如果存在,则将现有成员标签 (int)
ID 添加到 $team_member_tags_to_set
数组。如果没有找到现有的成员标签,我们只需将成员标题(名称)作为 (string)
添加到 $team_member_tags_to_set
数组。
完成所有这些后,我们只需使用 wp_set_post_tags()
传递我们的 $team_member_tags_to_set
数组来更新当前 projects
post 的 team_members
分类术语。
我还在 wp_set_post_tags()
中将 append 设置为 false,这会删除所有以前的标签并创建一组新标签。如果成员在 acf 关系字段中得到更新,这将有所帮助。
https://developer.wordpress.org/reference/functions/wp_set_post_tags/
查看下面的代码,并阅读我的评论,以便了解发生了什么。
<?php
// save post action
add_action('acf/save_post', 'set_employee_tags_on_save_update', 20);
/**
* @param $post_id int|string
*/
function set_employee_tags_on_save_update($post_id) {
// get our current post object
$post = get_post($post_id);
// if post is object
if(is_object($post)) {
// check we are on the projects custom type and post statuses are either publish or draft
// change 'projects' post type to your post type name which has the relationship field
if($post->post_type === 'projects' && ($post->post_status === 'publish' || $post->post_status === 'draft')) {
// get relationship field employees
// this example uses Post Object as the Return Format and is a multiple value
// change get field param 'employees' to your relationship field name
$employees = get_field('employees');
// team member tags to set empty array
$team_member_tags_to_set = [];
// if employees has value or values
if($employees) {
// get all of our current team member tags
// change 'team_members' taxonomy value to your members taxonomy name
$team_member_tags = get_terms([
'taxonomy' => 'team_members',
'orderby' => 'name',
'order' => 'ASC'
]);
// empty array for existing team member tags
$existing_team_member_tags = [];
// if we have existing team member tags
if(!empty($team_member_tags)) {
// foreach team member tags as team member tag
foreach($team_member_tags as $team_member_tag) {
// add existing team member to our existing team member tags array by tag ID => tag name
// this is so we can use this later to check if a team member tag already exists so we dont create duplicates
$existing_team_member_tags[$team_member_tag->ID] = $team_member_tag->name;
}
}
// foreach employees as employee
foreach($employees as $employee) {
// get the title for current employee
$title = get_the_title($employee->ID);
// search existing team members array and return the tag id via key
$existing_team_member_tag_id = array_search($title, $existing_team_member_tags);
// if we have an existing team member tag id
if($existing_team_member_tag_id) {
// add the existing team member tag id as integer to array
$team_member_tags_to_set[] = (int)$existing_team_member_tag_id;
} else {
// else create a new tag for team member by adding title (name) as string to array
$team_member_tags_to_set[] = (string)$title;
}
}
}
// remove the action
remove_action('acf/save_post', 'acf_save_post');
// set post tags for this post id, removing any unused team member tags if relationship field team members are changed
wp_set_post_tags($post_id, $team_member_tags_to_set, false);
// re add the action
add_action('acf/save_post', 'acf_save_post');
}
}
// finally return
return;
}
我正在为我们的项目页面创建自定义 post 类型。我还为我们的员工定制了 posts 类型。
在 ACF 中,我创建了一个关系字段,您可以在其中将团队成员添加到项目中,并显示在网站上。
基于关系字段中选定的团队成员 posts,我希望为关系字段中加载的每个职位(员工姓名)生成一个标签。
这是我现在卡住了。
PostObject中的名字叫做teamleden。我已尝试将代码添加到我的海关 posts 类型文件中,但它不起作用。
<?php
// save post action
add_action('acf/save_post', 'set_employee_tags_on_save_update', 20);
/**
* @param $post_id int|string
*/
function set_employee_tags_on_save_update($post_id) {
// get our current post object
$post = get_post($post_id);
// if post is object
if(is_object($post)) {
// check we are on the projects custom type and post statuses are either publish or draft
// change 'projects' post type to your post type name which has the relationship field
if($post->post_type === 'projects' && ($post->post_status === 'publish' || $post->post_status === 'draft')) {
// get relationship field employees
// this example uses Post Object as the Return Format and is a multiple value
// change get field param 'employees' to your relationship field name
$employees = get_field('employees');
// team member tags to set empty array
$team_member_tags_to_set = [];
// if employees has value or values
if($employees) {
// get all of our current team member tags
// change 'team_members' taxonomy value to your members taxonomy name
$team_member_tags = get_terms([
'taxonomy' => 'team_members',
'orderby' => 'name',
'order' => 'ASC'
]);
// empty array for existing team member tags
$existing_team_member_tags = [];
// if we have existing team member tags
if(!empty($team_member_tags)) {
// foreach team member tags as team member tag
foreach($team_member_tags as $team_member_tag) {
// add existing team member to our existing team member tags array by tag ID => tag name
// this is so we can use this later to check if a team member tag already exists so we dont create duplicates
$existing_team_member_tags[$team_member_tag->ID] = $team_member_tag->name;
}
}
// foreach employees as employee
foreach($employees as $employee) {
// get the title for current employee
$title = get_the_title($employee->ID);
// search existing team members array and return the tag id via key
$existing_team_member_tag_id = array_search($title, $existing_team_member_tags);
// if we have an existing team member tag id
if($existing_team_member_tag_id) {
// add the existing team member tag id as integer to array
$team_member_tags_to_set[] = (int)$existing_team_member_tag_id;
} else {
// else create a new tag for team member by adding title (name) as string to array
$team_member_tags_to_set[] = (string)$title;
}
}
}
// remove the action
remove_action('acf/save_post', 'acf_save_post');
// set post tags for this post id, removing any unused team member tags if relationship field team members are changed
wp_set_object_terms($post_id, $team_member_tags_to_set, $taxonomy = 'team_members', false);
// re add the action
add_action('acf/save_post', 'acf_save_post');
}
}
// finally return
return;
}
这还没有经过测试,但应该能让您走上正轨。
您需要将此添加到 function.php
。
参考您需要在我的示例代码中更改的内容...
projects
post_type 是 post 类型名称,此代码在保存或更新 post 时触发。employees
是projects
post类型中acf关系字段的名称。team_members
是您应该在projects
post 类型上使用的自定义标签分类法名称。
基本上,这段代码的作用是在使用 acf/save_post
操作保存、发布或更新 projects
post 时。它获取此 post 的 acf 关系字段成员数据。如果字段中有成员,它会获取所有现有的团队成员标签,并通过 ID => Title(name) 创建一个简单的现有成员标签数组。
然后遍历关系字段中的所有成员,并检查成员的标签是否已经存在,如果存在,则将现有成员标签 (int)
ID 添加到 $team_member_tags_to_set
数组。如果没有找到现有的成员标签,我们只需将成员标题(名称)作为 (string)
添加到 $team_member_tags_to_set
数组。
完成所有这些后,我们只需使用 wp_set_post_tags()
传递我们的 $team_member_tags_to_set
数组来更新当前 projects
post 的 team_members
分类术语。
我还在 wp_set_post_tags()
中将 append 设置为 false,这会删除所有以前的标签并创建一组新标签。如果成员在 acf 关系字段中得到更新,这将有所帮助。
https://developer.wordpress.org/reference/functions/wp_set_post_tags/
查看下面的代码,并阅读我的评论,以便了解发生了什么。
<?php
// save post action
add_action('acf/save_post', 'set_employee_tags_on_save_update', 20);
/**
* @param $post_id int|string
*/
function set_employee_tags_on_save_update($post_id) {
// get our current post object
$post = get_post($post_id);
// if post is object
if(is_object($post)) {
// check we are on the projects custom type and post statuses are either publish or draft
// change 'projects' post type to your post type name which has the relationship field
if($post->post_type === 'projects' && ($post->post_status === 'publish' || $post->post_status === 'draft')) {
// get relationship field employees
// this example uses Post Object as the Return Format and is a multiple value
// change get field param 'employees' to your relationship field name
$employees = get_field('employees');
// team member tags to set empty array
$team_member_tags_to_set = [];
// if employees has value or values
if($employees) {
// get all of our current team member tags
// change 'team_members' taxonomy value to your members taxonomy name
$team_member_tags = get_terms([
'taxonomy' => 'team_members',
'orderby' => 'name',
'order' => 'ASC'
]);
// empty array for existing team member tags
$existing_team_member_tags = [];
// if we have existing team member tags
if(!empty($team_member_tags)) {
// foreach team member tags as team member tag
foreach($team_member_tags as $team_member_tag) {
// add existing team member to our existing team member tags array by tag ID => tag name
// this is so we can use this later to check if a team member tag already exists so we dont create duplicates
$existing_team_member_tags[$team_member_tag->ID] = $team_member_tag->name;
}
}
// foreach employees as employee
foreach($employees as $employee) {
// get the title for current employee
$title = get_the_title($employee->ID);
// search existing team members array and return the tag id via key
$existing_team_member_tag_id = array_search($title, $existing_team_member_tags);
// if we have an existing team member tag id
if($existing_team_member_tag_id) {
// add the existing team member tag id as integer to array
$team_member_tags_to_set[] = (int)$existing_team_member_tag_id;
} else {
// else create a new tag for team member by adding title (name) as string to array
$team_member_tags_to_set[] = (string)$title;
}
}
}
// remove the action
remove_action('acf/save_post', 'acf_save_post');
// set post tags for this post id, removing any unused team member tags if relationship field team members are changed
wp_set_post_tags($post_id, $team_member_tags_to_set, false);
// re add the action
add_action('acf/save_post', 'acf_save_post');
}
}
// finally return
return;
}