简体   繁体   English

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

[英]Auto fill a Custom Post Type title from ACF field

I'd like to auto fill the title of three Custom Post Types (CPTs) based on an ACF field.我想根据 ACF 字段自动填充三个自定义帖子类型 (CPT) 的标题。 I found the code below, but don't know how to write it for three CPTs instead of just one.我找到了下面的代码,但不知道如何为三个 CPT 而不是一个编写它。 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);

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 WordPress 5.0.3
  2. Twentyninteen 1.2二十九 1.2
  3. Advanced Custom Fields PRO 5.7.10高级自定义字段 PRO 5.7.10
  4. Localhost (XAMPP for Windows 5.6.15)本地主机(适用于 Windows 5.6.15 的 XAMPP)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM