繁体   English   中英

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

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

我想根据 ACF 字段自动填充三个自定义帖子类型 (CPT) 的标题。 我找到了下面的代码,但不知道如何为三个 CPT 而不是一个编写它。 我将不胜感激!

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);

试试这个代码。 它应该适合你。

<?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 );

更新

将之前的代码替换为以下代码。 它将解决安德鲁在下面评论中指出的双重标题问题。

<?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 );

测试和工作:

  1. WordPress 5.0.3
  2. 二十九 1.2
  3. 高级自定义字段 PRO 5.7.10
  4. 本地主机(适用于 Windows 5.6.15 的 XAMPP)

暂无
暂无

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

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