我正在为我们的项目页面创建自定义帖子类型。我还为我们的员工定制了一个posts类型。
使用ACF,我制作了一个关系字段,您可以在其中将团队成员添加到项目中,并将其显示在网站上。
基于关系字段中选定的团队成员帖子,我希望为关系字段中加载的每个标题(员工名称)生成一个标签。
这就是我现在的处境。
Post对象中的名称称为teamleden。我尝试过向我的海关邮件类型文件中添加代码,但不起作用。
<?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\u type是保存或更新帖子时该代码触发的帖子类型名称。employees
是projects
职位类型中acf关系字段的名称。team\u members
是您在项目中应该使用的自定义标记分类名称
此代码的基本功能是使用
acf/save_post
操作保存、发布或更新projects
post。它获取此帖子的acf关系字段成员数据。如果字段中有成员,它将获取所有现有的团队成员标记,并按ID创建一个简单的现有成员标记数组=
然后,它循环遍历关系字段中的所有成员,并检查成员的标记是否已存在,如果已存在,则将现有成员标记
(int)
ID添加到$team\u member\u tags\u to\u set
数组中。如果没有找到现有的成员标记,我们只需将成员标题(名称)作为(字符串)
添加到$team\u member\u tags\u to\u set
数组中。
完成所有这些之后,我们只需使用
wp_set_post_tags()
传递我们的$team_member_tags_to_set
数组来更新当前项目
文章的team_members
分类法术语。
我还在
wp\u set\u post\u tags()
中设置了append to 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;
}
我正在努力通过JAVA自动生成SVN标记,需要一些建议。这就是我们手动操作的方式-从SVN repo中检查一个maven项目/插件,并运行一组maven命令(mvn clean test,mvn release:prepare)来生成SVN标记,mvn release:prepare是最后一个命令,它将运行单元测试,生成标记并将其提交给SVN,我正在努力实现这个过程的自动化。 我看了svnkit
我开始进入WP开发,我有问题与ACF后对象返回空,我不知道为什么。我创建了一个ACF,分类法的位置等于类别然后字段设置为... 字段名称:推荐的\u资源 然后在岗位上 我得到了无效的任何帮助或指向正确的方向都是非常感谢的。
下面的代码应该能够清楚地说明我想要实现的目标。关键问题在于meta_查询中的第二个数组。我正在尝试查找字段“alias”未设置post_对象的帖子。 使用var_dump(get_字段('alias')运行查询时;返回的结果为“NULL”。我不知道如何基于空的post对象字段进行查询。我非常感谢你的指点。
我试图使用一个图像从一个wordpress帖子作为自定义标题背景图像。我目前有它设置为使用特色图像(这工作,但我不想使用特色图像) HTML(header.php) 我想在帖子中针对不同的图像。不管它是第一个,还是我用某种方式标记的一个。只要我能控制一个习惯。 我已经安装了ACF插件,正如另一个人提到的,但我不知道如何使用它来做我想做的事情 编辑: 我希望每一篇文章都有不同的标题图片
我在WordPress与ACF合作。 我有一个自定义的帖子类型称为。在那里,用户可以选择通过ACF中继器字段上传2张特色图像。 现在,在主页上,我已经给用户提供了从项目帖子类型中选择8个帖子对象的选项。 我需要能够循环通过这个主页转发器字段,并从每个“项目”帖子对象中提取特征图像和项目标题。 ACF最近贬低了函数,我认为它把我扔在这里。 但是,到目前为止,我一直在努力解决以下问题: 我试图对代码进
当我使用Spring framework时,我经常看到2个术语基于Java和基于注释的配置/自动生成。 如果它们不一样,你能告诉我它们之间有什么不同吗?