Support

Account

Home Forums General Issues Taxonomy: getting ACF field from post terms

Unread

Taxonomy: getting ACF field from post terms

  • I’ve built a site with custom taxonomies for different project types.

    I use these taxonomy terms all over the site, including constructing default, generic titles for projects.

    Example:

    Project

    • Project Style: Bright
    • Project Type: Kitchen

    I use these terms to generate a default title: “A Bright Kitchen”

    ===

    I ended up adding an ACF Taxonomy Field as some of the plural terms were weird — porches vs porch, remodeling vs remodel, etc.

    I’ve successfully pulled this field into my “auto name generating” code, but I’m sure there is a better way to do it.

    <?php $project_types = wp_get_object_terms( $post->ID,  'project_type' );
    if ( ! empty( $project_types ) ) {
        if ( ! is_wp_error( $project_types ) ) {
    
                foreach( $project_types as $term ) {
                    $singular = get_field('singular', 'project_type_' . esc_html( $term->term_id ));
    
                }
    
        }
    } ?>
    
    <?php echo $singular ?>

    How can I simplify / improve this code?

Viewing 1 post (of 1 total)

You must be logged in to reply to this topic.