从 ACF 字段生成 WordPress 帖子标题

发布于 2025-01-16 07:53:01 字数 1771 浏览 6 评论 0原文

我正在尝试从两个 ACF 字段生成自定义帖子标题。我有 first_namelast_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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

衣神在巴黎 2025-01-23 07:53:01

我相信你的两个名字都是“first_name”。

$first_name = get_field('first_name', $post_id). ' ' . $value;
$last_name = get_field('first_name', $post_id). ' ' . $value;

You have "first_name" for both the names I believe.

$first_name = get_field('first_name', $post_id). ' ' . $value;
$last_name = get_field('first_name', $post_id). ' ' . $value;
森林很绿却致人迷途 2025-01-23 07:53:01

这是我在记住我在一个组中有 first_namelast_name 后想到的答案,这需要函数中的重复器字段:

function update_contacts_title($post_id) {

if( have_rows('contact_info') ):
    while( have_rows('contact_info') ): the_row(); 

        $first_name = get_sub_field('first_name', $post_id);
        $last_name = get_sub_field('last_name', $post_id);
        $title = $first_name . '-' . $last_name;
        $slug = sanitize_title( $title );

    endwhile;
endif;

    $postdata = array(
        'ID'          => $post_id,
        'post_title'  => $title,
        'post_type'   => 'contacts',
        'post_name'   => $slug
    );

    wp_update_post( $postdata );
    

}
add_filter('acf/save_post', 'update_contacts_title', 10, 3);

Here's the answer I figured out after I remembered that I have the first_name and last_name in a group, which requires a repeater field in the function:

function update_contacts_title($post_id) {

if( have_rows('contact_info') ):
    while( have_rows('contact_info') ): the_row(); 

        $first_name = get_sub_field('first_name', $post_id);
        $last_name = get_sub_field('last_name', $post_id);
        $title = $first_name . '-' . $last_name;
        $slug = sanitize_title( $title );

    endwhile;
endif;

    $postdata = array(
        'ID'          => $post_id,
        'post_title'  => $title,
        'post_type'   => 'contacts',
        'post_name'   => $slug
    );

    wp_update_post( $postdata );
    

}
add_filter('acf/save_post', 'update_contacts_title', 10, 3);

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文