donglefu6195 2019-01-17 22:21
浏览 109
已采纳

自动从ACF字段填写自定义帖子类型标题

I'd like to auto fill the title of three Custom Post Types (CPTs) based on an ACF field. I found the code below, but don't know how to write it for three CPTs instead of just one. I would appreciate the help!

function acf_title( $value, $post_id, $field ) {
    if ( get_post_type( $post_id ) == 'companies' ) {

        $new_title = get_field('company_name', $post_id) . ' ' . $value;
        $new_slug = sanitize_title( $new_title );

        wp_update_post( array(
            'ID'            => $post_id,
            'post_title'    => $new_title,
            'post_name'     => $new_slug,
            )
        );
    }
    return $value;
} 

add_filter('acf/update_value', 'acf_title', 10, 3);
  • 写回答

1条回答 默认 最新

  • dongxuan2015 2019-01-24 03:40
    关注

    Try this code. It should work for you.

    <?php
    // Auto fill Title and Slug for 'companies' CPT
    function acf_title_companies( $value, $post_id, $field ) {
        if ( get_post_type( $post_id ) == 'companies' ) {
            $new_title = get_field( 'company_name', $post_id ) . ' ' . $value;
            $new_slug = sanitize_title( $new_title );
            wp_update_post( array(
                'ID'         => $post_id,
                'post_title' => $new_title,
                'post_name'  => $new_slug,
                )
            );
        }
        return $value;
    } 
    add_filter( 'acf/update_value/name=company_name', 'acf_title_companies', 10, 3 );
    // Auto fill Title and Slug for 'contacts' CPT
    function acf_title_contacts( $value, $post_id, $field ) {
        if ( get_post_type( $post_id ) == 'contacts') {
            $new_title = get_field( 'name_first', $post_id ) . ' ' . $value;
            $new_slug = sanitize_title( $new_title );
            wp_update_post( array(
                'ID'         => $post_id,
                'post_title' => $new_title,
                'post_name'  => $new_slug,
                )
            );
        }
        return $value;
    } 
    add_filter( 'acf/update_value/name=name_first', 'acf_title_contacts', 10, 3 );
    // Auto fill Title and Slug for 'properties' CPT
    function acf_title_properties( $value, $post_id, $field ) {
        if ( get_post_type( $post_id ) == 'properties') {
            $new_title = get_field( 'building_name', $post_id ) . ' ' . $value;
            $new_slug = sanitize_title( $new_title );
            wp_update_post( array(
                'ID'         => $post_id,
                'post_title' => $new_title,
                'post_name'  => $new_slug,
                )
            );
        }
        return $value;
    } 
    add_filter( 'acf/update_value/name=building_name', 'acf_title_properties', 10, 3 );
    

    Update

    Replace the previous code with the following. It will resolve the double title issue as noted by Andrew in below comment.

    <?php
    
    // Auto fill Title and Slug for CPT's - 'companies', 'contacts', 'properties'
    function lh_acf_save_post( $post_id ) {
    
        // Don't do this on the ACF post type
        if ( get_post_type( $post_id ) == 'acf' ) {
            return;
        }
    
        $new_title = '';
        $new_slug = '';
    
        // Get title from 'companies' CPT acf field 'company_name'
        if ( get_post_type( $post_id ) == 'companies' ) {
            $new_title = get_field( 'company_name', $post_id );
            $new_slug = sanitize_title( $new_title );
        }
    
        // Get title from 'contacts'CPT acf field 'contact_name'
        if ( get_post_type( $post_id ) == 'contacts') {
            $contact_name = get_field( "contact_name" );
            if ( $contact_name ) {
                $name_first = $contact_name['name_first'];
                $name_last = $contact_name['name_last'];
            }
            $new_title = $name_first . ' ' . $name_last;
            $new_slug = sanitize_title( $new_title );
        }
    
        // Get title from 'properties' CPT acf field 'building_name'
        if ( get_post_type( $post_id ) == 'properties') {
            $new_title = get_field( 'building_name', $post_id );
            $new_slug = sanitize_title( $new_title );
        }
    
        // Prevent iInfinite looping...
        remove_action( 'acf/save_post', 'lh_acf_save_post' );
    
        // Grab post data
        $post = array(
            'ID'            => $post_id,
            'post_title'    => $new_title,
            'post_name'     => $new_slug,
        );
    
        // Update the Post
        wp_update_post( $post );
    
        // Continue save action
        add_action( 'acf/save_post', 'lh_save_post' );
    
        // Set the return URL in case of 'new' post
        $_POST['return'] = add_query_arg( 'updated', 'true', get_permalink( $post_id ) );
    }
    add_action( 'acf/save_post', 'lh_acf_save_post', 10, 1 );
    

    Tested and working on:

    1. WordPress 5.0.3
    2. Twentyninteen 1.2
    3. Advanced Custom Fields PRO 5.7.10
    4. Localhost (XAMPP for Windows 5.6.15)
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器