从 ACF 字段生成 WordPress 帖子标题
我正在尝试从两个 ACF 字段生成自定义帖子标题。我有 first_name
和 last_name
但它只会生成姓氏并执行两次:
/contacts/deere-deere/
这是我的函数:
function update_contacts_title( $value, $post_id, $field ) {
$first_name = get_field('first_name', $post_id). ' ' . $value;
$last_name = get_field('last_name', $post_id). ' ' . $value;
$title = $first_name .' - '. $last_name;
$slug = sanitize_title( $title );
$postdata = array(
'ID' => $post_id,
'post_title' => $title,
'post_type' => 'contacts',
'post_name' => $slug
);
wp_update_post( $postdata );
return $value;
}
add_filter('acf/update_value/name=first_name', 'update_contacts_title', 10, 3);
add_filter('acf/update_value/name=last_name', 'update_contacts_title', 10, 3);
如果我像这样修改这一行,那么它将生成 first_name
但仅生成一次: /contacts/john/
//Create new title based on ACF Fields
function update_contacts_title( $value, $post_id, $field ) {
$first_name = get_field('first_name', $post_id). ' ' . $value;
$last_name = get_field('last_name', $post_id). ' ' . $value;
//$title = $first_name .' - '. $last_name;
$title = $first_name;
$slug = sanitize_title( $title );
$postdata = array(
'ID' => $post_id,
'post_title' => $title,
'post_type' => 'contacts',
'post_name' => $slug
);
wp_update_post( $postdata );
return $value;
}
add_filter('acf/update_value/name=first_name', 'update_contacts_title', 10, 3);
//add_filter('acf/update_value/name=last_name', 'update_contacts_title', 10, 3);
这真的让我很困惑,为什么它不能识别名字和姓氏。
任何建议都会非常有帮助,谢谢大家。
I am trying to generate a custom post title from two ACF fields. I have first_name
and last_name
but it will only generate the last name and does it twice:
/contacts/deere-deere/
Here's my function:
function update_contacts_title( $value, $post_id, $field ) {
$first_name = get_field('first_name', $post_id). ' ' . $value;
$last_name = get_field('last_name', $post_id). ' ' . $value;
$title = $first_name .' - '. $last_name;
$slug = sanitize_title( $title );
$postdata = array(
'ID' => $post_id,
'post_title' => $title,
'post_type' => 'contacts',
'post_name' => $slug
);
wp_update_post( $postdata );
return $value;
}
add_filter('acf/update_value/name=first_name', 'update_contacts_title', 10, 3);
add_filter('acf/update_value/name=last_name', 'update_contacts_title', 10, 3);
If I modify this line down like so, then it will generate the first_name
but only once:/contacts/john/
//Create new title based on ACF Fields
function update_contacts_title( $value, $post_id, $field ) {
$first_name = get_field('first_name', $post_id). ' ' . $value;
$last_name = get_field('last_name', $post_id). ' ' . $value;
//$title = $first_name .' - '. $last_name;
$title = $first_name;
$slug = sanitize_title( $title );
$postdata = array(
'ID' => $post_id,
'post_title' => $title,
'post_type' => 'contacts',
'post_name' => $slug
);
wp_update_post( $postdata );
return $value;
}
add_filter('acf/update_value/name=first_name', 'update_contacts_title', 10, 3);
//add_filter('acf/update_value/name=last_name', 'update_contacts_title', 10, 3);
This is really confusing me as to why it won't recognize both the first name and last name.
Any suggestions would be extremely helpful, thank you all.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我相信你的两个名字都是“first_name”。
You have "first_name" for both the names I believe.
这是我在记住我在一个组中有
first_name
和last_name
后想到的答案,这需要函数中的重复器字段:Here's the answer I figured out after I remembered that I have the
first_name
andlast_name
in a group, which requires a repeater field in the function: